Skip to content

Commit

Permalink
Add problem 2357: Make Array Zero by Subtracting Equal Amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 10, 2024
1 parent 8f81160 commit 5389380
Show file tree
Hide file tree
Showing 3 changed files with 52 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 @@ -1748,6 +1748,7 @@ pub mod problem_2349_design_a_number_container_system;
pub mod problem_2351_first_letter_to_appear_twice;
pub mod problem_2352_equal_row_and_column_pairs;
pub mod problem_2353_design_a_food_rating_system;
pub mod problem_2357_make_array_zero_by_subtracting_equal_amounts;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub struct Solution;

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

impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
let mut seen = 0_u128;

for num in nums {
if num != 0 {
seen |= 1 << (num as u8);
}
}

seen.count_ones() as _
}
}

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

impl super::Solution for Solution {
fn minimum_operations(nums: Vec<i32>) -> i32 {
Self::minimum_operations(nums)
}
}

#[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 bit_masks;

pub trait Solution {
fn minimum_operations(nums: Vec<i32>) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [(&[1, 5, 0, 3, 5] as &[_], 3), (&[0], 0)];

for (nums, expected) in test_cases {
assert_eq!(S::minimum_operations(nums.to_vec()), expected);
}
}
}

0 comments on commit 5389380

Please sign in to comment.