1000. Minimum Cost to Merge Stones
Description
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
.
Example 1:
Input: stones = [3,2,4,1], k = 2 Output: 20 Explanation: We start with [3, 2, 4, 1]. We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1]. We merge [4, 1] for a cost of 5, and we are left with [5, 5]. We merge [5, 5] for a cost of 10, and we are left with [10]. The total cost was 20, and this is the minimum possible.
Example 2:
Input: stones = [3,2,4,1], k = 3 Output: -1 Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.
Example 3:
Input: stones = [3,5,1,2,6], k = 3 Output: 25 Explanation: We start with [3, 5, 1, 2, 6]. We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6]. We merge [3, 8, 6] for a cost of 17, and we are left with [17]. The total cost was 25, and this is the minimum possible.
Constraints:
n == stones.length
1 <= n <= 30
1 <= stones[i] <= 100
2 <= k <= 30
Solutions
Solution: Dynamic Programming
- Time complexity: O(n3/k)
- Space complexity: O(n2)
JavaScript
js
/**
* @param {number[]} stones
* @param {number} k
* @return {number}
*/
const mergeStones = function (stones, k) {
const n = stones.length;
if ((n - 1) % (k - 1)) return -1;
const prefixSum = new Array(n + 1).fill(0);
for (let index = 0; index < n; index++) {
prefixSum[index + 1] = prefixSum[index] + stones[index];
}
const dp = new Array(n)
.fill('')
.map(_ => new Array(n).fill(Number.MAX_SAFE_INTEGER));
for (let index = 0; index < n; index++) {
dp[index][index] = 0;
}
for (let length = 2; length <= n; length++) {
for (let left = 0; left + length <= n; left++) {
const right = left + length - 1;
for (let index = left; index < right; index += k - 1) {
dp[left][right] = Math.min(dp[left][index] + dp[index + 1][right], dp[left][right]);
}
if ((length - 1) % (k - 1) === 0) {
dp[left][right] += prefixSum[right + 1] - prefixSum[left];
}
}
}
return dp[0][n - 1];
};