Skip to content

Commit a7d1e56

Browse files
authoredMar 13, 2025
feat: add solutions to lc problem: No.1959 (#4155)
No.1959.Minimum Total Space Wasted With K Resizing Operations
1 parent e2ef4bd commit a7d1e56

File tree

4 files changed

+231
-2
lines changed

4 files changed

+231
-2
lines changed
 

‎solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README.md

+88
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ tags:
7474

7575
### 方法一:动态规划
7676

77+
题目等价于我们将数组 $\textit{nums}$ 分成 $k + 1$ 段,那么每一段的浪费空间为该段的最大值乘以该段的长度减去该段的元素之和。我们累加每一段的浪费空间,即可得到总浪费空间。我们将 $k$ 加 $1$,那么就相当于将数组分成 $k$ 段。
78+
79+
因此,我们定义数组 $\textit{g}[i][j]$ 表示 $\textit{nums}[i..j]$ 的最大值乘以 $\textit{nums}[i..j]$ 的长度减去 $\textit{nums}[i..j]$ 的元素之和。我们在 $[0, n)$ 的范围内枚举 $i$,在 $[i, n)$ 的范围内枚举 $j$,用一个变量 $s$ 维护 $\textit{nums}[i..j]$ 的元素之和,用一个变量 $\textit{mx}$ 维护 $\textit{nums}[i..j]$ 的最大值,那么我们可以得到:
80+
81+
$$
82+
\textit{g}[i][j] = \textit{mx} \times (j - i + 1) - s
83+
$$
84+
85+
接下来,我们定义 $\textit{f}[i][j]$ 表示前 $i$ 个元素分成 $j$ 段的最小浪费空间。我们初始化 $\textit{f}[0][0] = 0$,其余位置初始化为无穷大。我们在 $[1, n]$ 的范围内枚举 $i$,在 $[1, k]$ 的范围内枚举 $j$,然后我们枚举前 $j - 1$ 段的最后一个元素 $h$,那么有:
86+
87+
$$
88+
\textit{f}[i][j] = \min(\textit{f}[i][j], \
89+
\textit{f}[h][j - 1] + \textit{g}[h][i - 1])
90+
$$
91+
92+
最终答案为 $\textit{f}[n][k]$。
93+
94+
时间复杂度 $O(n^2 \times k)$,空间复杂度 $O(n \times (n + k))$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
95+
7796
<!-- tabs:start -->
7897

7998
#### Python3
@@ -203,6 +222,75 @@ func minSpaceWastedKResizing(nums []int, k int) int {
203222
}
204223
```
205224

225+
#### TypeScript
226+
227+
```ts
228+
function minSpaceWastedKResizing(nums: number[], k: number): number {
229+
k += 1;
230+
const n = nums.length;
231+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
232+
233+
for (let i = 0; i < n; i++) {
234+
let s = 0,
235+
mx = 0;
236+
for (let j = i; j < n; j++) {
237+
s += nums[j];
238+
mx = Math.max(mx, nums[j]);
239+
g[i][j] = mx * (j - i + 1) - s;
240+
}
241+
}
242+
243+
const inf = Number.POSITIVE_INFINITY;
244+
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf));
245+
f[0][0] = 0;
246+
247+
for (let i = 1; i <= n; i++) {
248+
for (let j = 1; j <= k; j++) {
249+
for (let h = 0; h < i; h++) {
250+
f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]);
251+
}
252+
}
253+
}
254+
255+
return f[n][k];
256+
}
257+
```
258+
259+
#### Rust
260+
261+
```rust
262+
impl Solution {
263+
pub fn min_space_wasted_k_resizing(nums: Vec<i32>, k: i32) -> i32 {
264+
let mut k = k + 1;
265+
let n = nums.len();
266+
let mut g = vec![vec![0; n]; n];
267+
268+
for i in 0..n {
269+
let (mut s, mut mx) = (0, 0);
270+
for j in i..n {
271+
s += nums[j];
272+
mx = mx.max(nums[j]);
273+
g[i][j] = mx * (j as i32 - i as i32 + 1) - s;
274+
}
275+
}
276+
277+
let inf = 0x3f3f3f3f;
278+
let mut f = vec![vec![inf; (k + 1) as usize]; n + 1];
279+
f[0][0] = 0;
280+
281+
for i in 1..=n {
282+
for j in 1..=k as usize {
283+
for h in 0..i {
284+
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
285+
}
286+
}
287+
}
288+
289+
f[n][k as usize]
290+
}
291+
}
292+
```
293+
206294
<!-- tabs:end -->
207295

208296
<!-- solution:end -->

‎solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README_EN.md

+84-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The total wasted space is (20 - 10) + (20 - 20) = 10.
4444
<strong>Input:</strong> nums = [10,20,30], k = 1
4545
<strong>Output:</strong> 10
4646
<strong>Explanation:</strong> size = [20,20,30].
47-
We can set the initial size to be 20 and resize to 30 at time 2.
47+
We can set the initial size to be 20 and resize to 30 at time 2.
4848
The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.
4949
</pre>
5050

@@ -73,7 +73,20 @@ The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 -
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
Solution 1: Dynamic Programming
77+
The problem is equivalent to dividing the array $\textit{nums}$ into $k + 1$ segments. The wasted space for each segment is the maximum value of that segment multiplied by the length of the segment minus the sum of the elements in that segment. By summing the wasted space of each segment, we get the total wasted space. By adding 1 to $k$, we are effectively dividing the array into $k$ segments.
78+
79+
Therefore, we define an array $\textit{g}[i][j]$ to represent the wasted space for the segment $\textit{nums}[i..j]$, which is the maximum value of $\textit{nums}[i..j]$ multiplied by the length of $\textit{nums}[i..j]$ minus the sum of the elements in $\textit{nums}[i..j]$. We iterate over $i$ in the range $[0, n)$ and $j$ in the range $[i, n)$, using a variable $s$ to maintain the sum of the elements in $\textit{nums}[i..j]$ and a variable $\textit{mx}$ to maintain the maximum value of $\textit{nums}[i..j]$. Then we can get:
80+
81+
$$ \textit{g}[i][j] = \textit{mx} \times (j - i + 1) - s $$
82+
83+
Next, we define $\textit{f}[i][j]$ to represent the minimum wasted space for dividing the first $i$ elements into $j$ segments. We initialize $\textit{f}[0][0] = 0$ and the other positions to infinity. We iterate over $i$ in the range $[1, n]$ and $j$ in the range $[1, k]$, then we iterate over the last element $h$ of the previous $j - 1$ segments. Then we have:
84+
85+
$$ \textit{f}[i][j] = \min(\textit{f}[i][j], \textit{f}[h][j - 1] + \textit{g}[h][i - 1]) $$
86+
87+
The final answer is $\textit{f}[n][k]$.
88+
89+
The time complexity is $O(n^2 \times k)$, and the space complexity is $O(n \times (n + k))$. Where $n$ is the length of the array $\textit{nums}$.
7790

7891
<!-- tabs:start -->
7992

@@ -204,6 +217,75 @@ func minSpaceWastedKResizing(nums []int, k int) int {
204217
}
205218
```
206219

220+
#### TypeScript
221+
222+
```ts
223+
function minSpaceWastedKResizing(nums: number[], k: number): number {
224+
k += 1;
225+
const n = nums.length;
226+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
227+
228+
for (let i = 0; i < n; i++) {
229+
let s = 0,
230+
mx = 0;
231+
for (let j = i; j < n; j++) {
232+
s += nums[j];
233+
mx = Math.max(mx, nums[j]);
234+
g[i][j] = mx * (j - i + 1) - s;
235+
}
236+
}
237+
238+
const inf = Number.POSITIVE_INFINITY;
239+
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf));
240+
f[0][0] = 0;
241+
242+
for (let i = 1; i <= n; i++) {
243+
for (let j = 1; j <= k; j++) {
244+
for (let h = 0; h < i; h++) {
245+
f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]);
246+
}
247+
}
248+
}
249+
250+
return f[n][k];
251+
}
252+
```
253+
254+
#### Rust
255+
256+
```rust
257+
impl Solution {
258+
pub fn min_space_wasted_k_resizing(nums: Vec<i32>, k: i32) -> i32 {
259+
let mut k = k + 1;
260+
let n = nums.len();
261+
let mut g = vec![vec![0; n]; n];
262+
263+
for i in 0..n {
264+
let (mut s, mut mx) = (0, 0);
265+
for j in i..n {
266+
s += nums[j];
267+
mx = mx.max(nums[j]);
268+
g[i][j] = mx * (j as i32 - i as i32 + 1) - s;
269+
}
270+
}
271+
272+
let inf = 0x3f3f3f3f;
273+
let mut f = vec![vec![inf; (k + 1) as usize]; n + 1];
274+
f[0][0] = 0;
275+
276+
for i in 1..=n {
277+
for j in 1..=k as usize {
278+
for h in 0..i {
279+
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
280+
}
281+
}
282+
}
283+
284+
f[n][k as usize]
285+
}
286+
}
287+
```
288+
207289
<!-- tabs:end -->
208290

209291
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn min_space_wasted_k_resizing(nums: Vec<i32>, k: i32) -> i32 {
3+
let mut k = k + 1;
4+
let n = nums.len();
5+
let mut g = vec![vec![0; n]; n];
6+
7+
for i in 0..n {
8+
let (mut s, mut mx) = (0, 0);
9+
for j in i..n {
10+
s += nums[j];
11+
mx = mx.max(nums[j]);
12+
g[i][j] = mx * (j as i32 - i as i32 + 1) - s;
13+
}
14+
}
15+
16+
let inf = 0x3f3f3f3f;
17+
let mut f = vec![vec![inf; (k + 1) as usize]; n + 1];
18+
f[0][0] = 0;
19+
20+
for i in 1..=n {
21+
for j in 1..=k as usize {
22+
for h in 0..i {
23+
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
24+
}
25+
}
26+
}
27+
28+
f[n][k as usize]
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function minSpaceWastedKResizing(nums: number[], k: number): number {
2+
k += 1;
3+
const n = nums.length;
4+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
5+
6+
for (let i = 0; i < n; i++) {
7+
let s = 0,
8+
mx = 0;
9+
for (let j = i; j < n; j++) {
10+
s += nums[j];
11+
mx = Math.max(mx, nums[j]);
12+
g[i][j] = mx * (j - i + 1) - s;
13+
}
14+
}
15+
16+
const inf = Number.POSITIVE_INFINITY;
17+
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf));
18+
f[0][0] = 0;
19+
20+
for (let i = 1; i <= n; i++) {
21+
for (let j = 1; j <= k; j++) {
22+
for (let h = 0; h < i; h++) {
23+
f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]);
24+
}
25+
}
26+
}
27+
28+
return f[n][k];
29+
}

0 commit comments

Comments
 (0)