Skip to content

Commit

Permalink
Add problem 2048: Next Greater Numerically Balanced Number
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Aug 6, 2024
1 parent 382c105 commit caa050b
Show file tree
Hide file tree
Showing 3 changed files with 58 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 @@ -1507,6 +1507,7 @@ pub mod problem_2042_check_if_numbers_are_ascending_in_a_sentence;
pub mod problem_2043_simple_bank_system;
pub mod problem_2044_count_number_of_maximum_bitwise_or_subsets;
pub mod problem_2047_number_of_valid_words_in_a_sentence;
pub mod problem_2048_next_greater_numerically_balanced_number;
pub mod problem_2049_count_nodes_with_the_highest_score;
pub mod problem_2053_kth_distinct_string_in_an_array;
pub mod problem_2054_two_best_non_overlapping_events;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub struct Solution;

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

impl Solution {
pub fn next_beautiful_number(n: i32) -> i32 {
const DATA: [u32; 109] = [
1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, 4444, 14444, 22333, 23233, 23323, 23332, 32233, 32323,
32332, 33223, 33232, 33322, 41444, 44144, 44414, 44441, 55555, 122_333, 123_233, 123_323, 123_332, 132_233,
132_323, 132_332, 133_223, 133_232, 133_322, 155_555, 212_333, 213_233, 213_323, 213_332, 221_333, 223_133,
223_313, 223_331, 224_444, 231_233, 231_323, 231_332, 232_133, 232_313, 232_331, 233_123, 233_132, 233_213,
233_231, 233_312, 233_321, 242_444, 244_244, 244_424, 244_442, 312_233, 312_323, 312_332, 313_223, 313_232,
313_322, 321_233, 321_323, 321_332, 322_133, 322_313, 322_331, 323_123, 323_132, 323_213, 323_231, 323_312,
323_321, 331_223, 331_232, 331_322, 332_123, 332_132, 332_213, 332_231, 332_312, 332_321, 333_122, 333_212,
333_221, 422_444, 424_244, 424_424, 424_442, 442_244, 442_424, 442_442, 444_224, 444_242, 444_422, 515_555,
551_555, 555_155, 555_515, 555_551, 666_666,
];

let i = DATA.partition_point(|&x| x <= n as u32);

DATA.get(i).copied().unwrap_or(1_224_444) as _
}
}

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

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

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

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

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

pub fn run<S: Solution>() {
let test_cases = [(1, 22), (1000, 1333), (3000, 3133)];

for (n, expected) in test_cases {
assert_eq!(S::next_beautiful_number(n), expected);
}
}
}

0 comments on commit caa050b

Please sign in to comment.