Skip to content

Commit

Permalink
Add problem 1959: Minimum Total Space Wasted With K Resizing Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed May 23, 2024
1 parent b42da7a commit 365741a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__*
Cargo.lock
.vscode/
/__*
/coverage/
/target/
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@ pub mod problem_1946_largest_number_after_mutating_substring;
pub mod problem_1952_three_divisors;
pub mod problem_1957_delete_characters_to_make_fancy_string;
pub mod problem_1958_check_if_move_is_legal;
pub mod problem_1959_minimum_total_space_wasted_with_k_resizing_operations;
pub mod problem_1961_check_if_string_is_a_prefix_of_array;
pub mod problem_1962_remove_stones_to_minimize_the_total;
pub mod problem_1963_minimum_number_of_swaps_to_make_the_string_balanced;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub struct Solution;

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

impl Solution {
pub fn min_space_wasted_k_resizing(nums: Vec<i32>, k: i32) -> i32 {
let n = nums.len();
let mut cache = vec![0; n].into_boxed_slice();
let k = k as u32 as usize;

let mut max = 0;
let mut count = 0;
let mut total = 0;

for (target, &num) in cache.iter_mut().zip(&nums) {
let num = num as u32;

max = u32::max(max, num);
count += 1;
*target = max * count;

total += num;
}

for resize_count in 1..=k {
for last_index in (resize_count..n).rev() {
let mut max = 0;
let mut count = 0;
let mut min_capacity = u32::MAX;

for i in (resize_count..=last_index).rev() {
max = u32::max(max, nums[i] as _);
count += 1;
min_capacity = u32::min(min_capacity, cache[i - 1] + max * count);
}

cache[last_index] = min_capacity;
}
}

(cache.last().unwrap() - total) as _
}
}

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

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

#[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,22 @@
pub mod dynamic_programming;

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

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

pub fn run<S: Solution>() {
let test_cases = [
((&[10, 20] as &[_], 0), 10),
((&[10, 20, 30], 1), 10),
((&[10, 20, 15, 30, 20], 2), 15),
];

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

0 comments on commit 365741a

Please sign in to comment.