-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1959: Minimum Total Space Wasted With K Resizing Operations
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__* | ||
Cargo.lock | ||
.vscode/ | ||
/__* | ||
/coverage/ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...problem_1959_minimum_total_space_wasted_with_k_resizing_operations/dynamic_programming.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1959_minimum_total_space_wasted_with_k_resizing_operations/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |