Skip to content

Commit

Permalink
Add problem 1962: Remove Stones to Minimize the Total
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 17, 2023
1 parent d5856ec commit 16d670e
Show file tree
Hide file tree
Showing 3 changed files with 54 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 @@ -1480,6 +1480,7 @@ pub mod problem_1945_sum_of_digits_of_string_after_convert;
pub mod problem_1952_three_divisors;
pub mod problem_1957_delete_characters_to_make_fancy_string;
pub mod problem_1961_check_if_string_is_a_prefix_of_array;
pub mod problem_1962_remove_stones_to_minimize_the_total;

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

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

use std::collections::BinaryHeap;

impl Solution {
pub fn min_stone_sum(piles: Vec<i32>, k: i32) -> i32 {
let mut queue = BinaryHeap::from(piles);

for _ in 0..k {
let num = &mut *queue.peek_mut().unwrap();

*num = (*num + 1) / 2;
}

queue.iter().sum()
}
}

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

impl super::Solution for Solution {
fn min_stone_sum(piles: Vec<i32>, k: i32) -> i32 {
Self::min_stone_sum(piles, k)
}
}

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

pub trait Solution {
fn min_stone_sum(piles: Vec<i32>, k: i32) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [((&[5, 4, 9] as &[_], 2), 12), ((&[4, 3, 6, 7], 3), 12)];

for ((piles, k), expected) in test_cases {
assert_eq!(S::min_stone_sum(piles.to_vec(), k), expected);
}
}
}

0 comments on commit 16d670e

Please sign in to comment.