-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1000-minimum-cost-to-merge-stones.js
50 lines (43 loc) · 1.34 KB
/
1000-minimum-cost-to-merge-stones.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 1000. Minimum Cost to Merge Stones
* https://leetcode.com/problems/minimum-cost-to-merge-stones/
* Difficulty: Hard
*
* There are n piles of stones arranged in a row. The ith pile has stones[i] stones.
*
* A move consists of merging exactly k consecutive piles into one pile, and the cost of
* this move is equal to the total number of stones in these k piles.
*
* Return the minimum cost to merge all piles of stones into one pile. If it is impossible,
* return -1.
*/
/**
* @param {number[]} stones
* @param {number} k
* @return {number}
*/
var mergeStones = function(stones, k) {
const n = stones.length;
if ((n - 1) % (k - 1) !== 0) return -1;
const prefixSums = new Array(n + 1).fill(0);
for (let i = 0; i < n; i++) {
prefixSums[i + 1] = prefixSums[i] + stones[i];
}
const dp = new Array(n).fill().map(() => new Array(n).fill(0));
for (let len = k; len <= n; len++) {
for (let start = 0; start + len <= n; start++) {
const end = start + len - 1;
dp[start][end] = Infinity;
for (let mid = start; mid < end; mid += k - 1) {
dp[start][end] = Math.min(
dp[start][end],
dp[start][mid] + dp[mid + 1][end]
);
}
if ((len - 1) % (k - 1) === 0) {
dp[start][end] += prefixSums[end + 1] - prefixSums[start];
}
}
}
return dp[0][n - 1];
};