Skip to content

Commit 7b2e52e

Browse files
authored
Update 1959-minimum-total-space-wasted-with-k-resizing-operations.js
1 parent ceee262 commit 7b2e52e

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

1959-minimum-total-space-wasted-with-k-resizing-operations.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
* @return {number}
55
*/
66
const minSpaceWastedKResizing = function(nums, k) {
7-
const n = nums.length, INF = 200 * 1e6;
8-
const memo = Array.from({ length: 200 }, () => Array(200));
9-
return dp(nums, 0, k);
10-
function dp(nums, i, k) {
11-
if (i === n) return 0;
12-
if (k === -1) return INF;
13-
if (memo[i][k] != null) return memo[i][k];
14-
let ans = INF, maxNum = nums[i], totalSum = 0;
15-
for (let j = i; j < n; j++) {
16-
maxNum = Math.max(maxNum, nums[j]);
17-
totalSum += nums[j];
18-
const wasted = maxNum * (j - i + 1) - totalSum;
19-
ans = Math.min(ans, dp(nums, j + 1, k - 1) + wasted);
7+
const n = nums.length, INF = 200 * 1e6
8+
const memo = Array.from({length: n}, () => Array(k))
9+
return dp(0, k)
10+
11+
function dp(i, k) {
12+
if(i === n) return 0
13+
if(k < 0) return INF
14+
if(memo[i][k] != null) return memo[i][k]
15+
let res = INF, max = nums[i], sum = 0
16+
for(let j = i; j < n; j++) {
17+
max = Math.max(max, nums[j])
18+
sum += nums[j]
19+
const waste = max * (j - i + 1) - sum
20+
res = Math.min(res, dp(j + 1, k - 1) + waste)
2021
}
21-
return memo[i][k] = ans;
22+
return memo[i][k] = res
2223
}
2324
};
24-

0 commit comments

Comments
 (0)