Skip to content

Commit

Permalink
Add problem 2222: Number of Ways to Select Buildings
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jul 12, 2024
1 parent 1fac262 commit 8346a89
Show file tree
Hide file tree
Showing 4 changed files with 97 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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
19 changes: 19 additions & 0 deletions src/problem_2222_number_of_ways_to_select_buildings/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
let test_cases = [("001101", 6), ("11100", 0)];

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

0 comments on commit 8346a89

Please sign in to comment.