Minimum Cost to Merge Stones - Problem
Minimum Cost to Merge Stones

You're given n piles of stones arranged in a row, where the i-th pile contains stones[i] stones. Your task is to merge all piles into a single pile with minimum cost.

šŸ“‹ Rules:
• Each move merges exactly k consecutive piles into one pile
• The cost of merging equals the total stones in those k piles
• You must merge ALL piles into ONE final pile

šŸŽÆ Goal: Return the minimum cost to merge all stones, or -1 if impossible.

Example: With stones = [3,2,4,1] and k = 2:
• Merge piles 1&2: cost = 3+2 = 5, result = [5,4,1]
• Merge piles 1&2: cost = 5+4 = 9, result = [9,1]
• Merge piles 1&2: cost = 9+1 = 10, result = [10]
• Total cost = 5+9+10 = 24

Input & Output

example_1.py — Basic Case
$ Input: stones = [3,2,4,1], k = 2
› Output: 20
šŸ’” Note: Step 1: Merge piles 1&2: [3,2,4,1] → [5,4,1], cost = 5. Step 2: Merge piles 2&3: [5,4,1] → [5,5], cost = 9. Step 3: Merge piles 1&2: [5,5] → [10], cost = 10. Total cost = 5 + 9 + 10 = 24. But there's an optimal path: Step 1: [3,2,4,1] → [3,6,1], cost = 6. Step 2: [3,6,1] → [9,1], cost = 9. Step 3: [9,1] → [10], cost = 10. Total = 20.
example_2.py — Impossible Case
$ Input: stones = [3,2,4,1], k = 3
› Output: -1
šŸ’” Note: With k=3, we need to merge 3 piles at a time. Starting with 4 piles, after one merge we have 2 piles. But we can't merge 2 piles with k=3. The feasibility condition (n-1) % (k-1) = (4-1) % (3-1) = 3 % 2 = 1 ≠ 0, so it's impossible.
example_3.py — Single Pile
$ Input: stones = [6], k = 3
› Output: 0
šŸ’” Note: Already have just one pile, so no merging needed. Cost is 0.

Constraints

  • 1 ≤ stones.length ≤ 30
  • 1 ≤ stones[i] ≤ 100
  • 2 ≤ k ≤ stones.length
  • Key constraint: Solution exists only if (n-1) % (k-1) == 0

Visualization

Tap to expand
Stone Merging: From Multiple Piles to OneInput: stones = [3, 2, 4, 1], k = 2Step 1: Check Feasibility(n-1) % (k-1) = (4-1) % (2-1) = 0 āœ“Merging is possible!Step 2: Initial State3241Step 3: Optimal Merge SequenceMerge [2,4] → cost = 6361Cost: 6Merge [3,6] → cost = 991Cost: 9Merge [9,1] → cost = 1010Cost: 10Total Cost Calculation:6 (merge [2,4]) + 9 (merge [3,6]) + 10 (merge [9,1])= 20 (minimum cost)DP State Representation:dp[i][j][p] = min cost to mergestones[i:j+1] into p pilesKey insight:dp[i][j][1] = dp[i][j][k] + sum(i to j)First create k piles, then merge to 1Time: O(n³k), Space: O(n²k)Why This Works:• Mathematical constraint ensures feasibility• Interval DP captures optimal substructure• 3D state tracks both range and pile count• Prefix sums enable O(1) range sum queries
Understanding the Visualization
1
Check Feasibility
Verify that merging is mathematically possible using (n-1) % (k-1) == 0
2
Build Prefix Sums
Create prefix sum array for O(1) range sum queries during DP
3
Initialize Base Cases
Single piles (dp[i][i][1]) require 0 cost to 'merge' into 1 pile
4
Fill DP Table
For each interval [i,j], compute minimum cost to create p piles
5
Merge to One Pile
To get 1 pile from interval [i,j]: first create k piles, then merge them
Key Takeaway
šŸŽÆ Key Insight: This is an interval DP problem where we must first verify feasibility using (n-1) % (k-1) == 0, then use 3D dynamic programming to track the minimum cost of merging any subrange into any number of piles, ultimately building up to the answer.
Asked in
Google 25 Amazon 18 Meta 12 Apple 8
28.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen