Skip to content

Commit

Permalink
Add problem 1689: Partitioning Into Minimum Number Of Deci-Binary Num…
Browse files Browse the repository at this point in the history
…bers
  • Loading branch information
EFanZh committed Jul 4, 2023
1 parent fa93583 commit 37fa427
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ pub mod problem_1678_goal_parser_interpretation;
pub mod problem_1679_max_number_of_k_sum_pairs;
pub mod problem_1684_count_the_number_of_consistent_strings;
pub mod problem_1688_count_of_matches_in_tournament;
pub mod problem_1689_partitioning_into_minimum_number_of_deci_binary_numbers;
pub mod problem_1695_maximum_erasure_value;
pub mod problem_1700_number_of_students_unable_to_eat_lunch;
pub mod problem_1701_average_waiting_time;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn min_partitions(n: String) -> i32 {
let mut result = b'0';

for c in n.bytes() {
if c > result {
result = c;

if result == b'9' {
break;
}
}
}

i32::from(result - b'0')
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn min_partitions(n: String) -> i32 {
Self::min_partitions(n)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod greedy;

pub trait Solution {
fn min_partitions(n: String) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("32", 3), ("82734", 8), ("27346209830709182346", 9)];

for (n, expected) in test_cases {
assert_eq!(S::min_partitions(n.to_string()), expected);
}
}
}

0 comments on commit 37fa427

Please sign in to comment.