Skip to content

Commit

Permalink
Add problem 1323: Maximum 69 Number
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Dec 15, 2024
1 parent 94f6d10 commit 96dfe01
Show file tree
Hide file tree
Showing 3 changed files with 60 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 @@ -537,6 +537,7 @@ pub mod problem_1304_find_n_unique_integers_sum_up_to_zero;
pub mod problem_1310_xor_queries_of_a_subarray;
pub mod problem_1315_sum_of_nodes_with_even_valued_grandparent;
pub mod problem_1317_convert_integer_to_the_sum_of_two_no_zero_integers;
pub mod problem_1323_maximum_69_number;
pub mod problem_1324_print_words_vertically;
pub mod problem_1325_delete_leaves_with_a_given_value;
pub mod problem_1333_filter_restaurants_by_vegan_friendly_price_and_distance;
Expand Down
41 changes: 41 additions & 0 deletions src/problem_1323_maximum_69_number/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub struct Solution;

impl Solution {
pub fn maximum69_number(num: i32) -> i32 {
let mut result = 0;
let mut div = if num < 100 {
10
} else if num < 1000 {
100
} else {
1000
};
let mut num = num;
let mut ok = false;
while num != 0 {
let mut digit = num / div;
num %= div;
div /= 10;
if digit == 6 && !ok {
ok = true;
digit = 9;
}
result = result * 10 + digit;
}
result
}
}

impl super::Solution for Solution {
fn maximum69_number(num: i32) -> i32 {
Self::maximum69_number(num)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_1323_maximum_69_number/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn maximum69_number(num: i32) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [(9669, 9969), (9996, 9999), (9999, 9999)];

for (num, expected) in test_cases {
assert_eq!(S::maximum69_number(num), expected);
}
}
}

0 comments on commit 96dfe01

Please sign in to comment.