Minimum Cost To Merge Stones Problem
Description
LeetCode Problem 1000.
There are n piles of stones arranged in a row. The i^th 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
Solution
Approach: Interval Dynamic Programming
First, feasibility. Every move turns k piles into one, so it removes exactly k - 1 piles. Going from n piles to 1 is possible only if (n - 1) % (k - 1) == 0; otherwise return -1.
Now let cost[i][j] be the minimum cost to merge stones[i..j] into as few piles as possible. A range of length L always ends with (L - 1) % (k - 1) + 1 piles, so this number is fixed and we only minimize the cost.
To fill cost[i][j], look at the first pile of the final result: it comes from a prefix stones[i..mid] that was merged into exactly one pile. Such a prefix has length 1 + m * (k - 1), so mid runs from i to j - 1 in steps of k - 1:
cost[i][j] = min(cost[i][mid] + cost[mid + 1][j])over thosemid.- If the whole range can become a single pile (
(L - 1) % (k - 1) == 0), the parts end as exactlykpiles, and one more merge combines them. That merge costs the sum ofstones[i..j], whatever the order of earlier merges, so add it using prefix sums.
Fill the table by increasing length; single piles cost 0. The answer is cost[0][n - 1].
The step people get wrong is the mid stride: with step 1, the left part may end as several piles, and the two parts together can then have more piles than the range should end with, which gives a cost that is too small. Stepping by k - 1 keeps the left part at exactly one pile.
Complexity: O(n3 / k) time and O(n2) space, where n is the length of stones.
class Solution:
def mergeStones(self, stones: List[int], k: int) -> int:
n = len(stones)
# Each move turns k piles into 1, so it removes k - 1 piles.
if (n - 1) % (k - 1) != 0:
return -1
prefix = [0] * (n + 1) # prefix[i] = stones[0] + ... + stones[i - 1]
for i in range(n):
prefix[i + 1] = prefix[i] + stones[i]
# cost[i][j] = min cost to merge stones[i..j] into as few piles as possible.
cost = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
best = float("inf")
# The left part stones[i..mid] ends as exactly one pile, so its length is 1 + m * (k - 1).
for mid in range(i, j, k - 1):
best = min(best, cost[i][mid] + cost[mid + 1][j])
# If the range can become a single pile, the last merge costs the whole range's sum.
if (length - 1) % (k - 1) == 0:
best += prefix[j + 1] - prefix[i]
cost[i][j] = best
return cost[0][n - 1]
Now write it from memory
Close the write-up and solve it yourself against the tests.