From 8346a8938b481d530dddbff613e103d36c1df2ab Mon Sep 17 00:00:00 2001 From: EFanZh Date: Fri, 12 Jul 2024 21:54:15 +0800 Subject: [PATCH] Add problem 2222: Number of Ways to Select Buildings --- src/lib.rs | 1 + .../dynamic_programming.rs | 43 +++++++++++++++++++ .../dynamic_programming_2.rs | 34 +++++++++++++++ .../mod.rs | 19 ++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming.rs create mode 100644 src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming_2.rs create mode 100644 src/problem_2222_number_of_ways_to_select_buildings/mod.rs diff --git a/src/lib.rs b/src/lib.rs index acfb3777..580c54ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1576,6 +1576,7 @@ pub mod problem_2210_count_hills_and_valleys_in_an_array; pub mod problem_2215_find_the_difference_of_two_arrays; pub mod problem_2216_minimum_deletions_to_make_array_beautiful; pub mod problem_2220_minimum_bit_flips_to_convert_number; +pub mod problem_2222_number_of_ways_to_select_buildings; pub mod problem_2224_minimum_number_of_operations_to_convert_time; pub mod problem_2225_find_players_with_zero_or_one_losses; pub mod problem_2226_maximum_candies_allocated_to_k_children; diff --git a/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming.rs b/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming.rs new file mode 100644 index 00000000..58a45cc8 --- /dev/null +++ b/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming.rs @@ -0,0 +1,43 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn number_of_ways(s: String) -> i64 { + let mut zero = 0_u32; + let mut one_zero = 0_u32; + let mut one = 0_u32; + let mut zero_one = 0_u32; + + s.into_bytes().into_iter().fold(0_u64, |result, c| { + result + + u64::from(if c == b'0' { + zero += 1; + one_zero += one; + + zero_one + } else { + one += 1; + zero_one += zero; + + one_zero + }) + }) as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn number_of_ways(s: String) -> i64 { + Self::number_of_ways(s) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming_2.rs b/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming_2.rs new file mode 100644 index 00000000..034882f4 --- /dev/null +++ b/src/problem_2222_number_of_ways_to_select_buildings/dynamic_programming_2.rs @@ -0,0 +1,34 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn number_of_ways(s: String) -> i64 { + let mut states = [(0_u32, 0_u32), (0_u32, 0_u32)]; + + s.into_bytes().into_iter().fold(0_u64, |result, c| { + let c = usize::from(c & 1); + + states[c].0 += 1; + states[c].1 += states[1 - c].0; + + result + u64::from(states[1 - c].1) + }) as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn number_of_ways(s: String) -> i64 { + Self::number_of_ways(s) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2222_number_of_ways_to_select_buildings/mod.rs b/src/problem_2222_number_of_ways_to_select_buildings/mod.rs new file mode 100644 index 00000000..f7c4c988 --- /dev/null +++ b/src/problem_2222_number_of_ways_to_select_buildings/mod.rs @@ -0,0 +1,19 @@ +pub mod dynamic_programming; +pub mod dynamic_programming_2; + +pub trait Solution { + fn number_of_ways(s: String) -> i64; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("001101", 6), ("11100", 0)]; + + for (s, expected) in test_cases { + assert_eq!(S::number_of_ways(s.to_string()), expected); + } + } +}