|
| 1 | +## 1000. Minimum Cost to Merge Stones |
| 2 | + |
| 3 | +### Question |
| 4 | +There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Find the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1. |
| 9 | + |
| 10 | +``` |
| 11 | +Example 1: |
| 12 | +
|
| 13 | +Input: stones = [3,2,4,1], K = 2 |
| 14 | +Output: 20 |
| 15 | +Explanation: |
| 16 | +We start with [3, 2, 4, 1]. |
| 17 | +We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1]. |
| 18 | +We merge [4, 1] for a cost of 5, and we are left with [5, 5]. |
| 19 | +We merge [5, 5] for a cost of 10, and we are left with [10]. |
| 20 | +The total cost was 20, and this is the minimum possible. |
| 21 | +
|
| 22 | +Example 2: |
| 23 | +
|
| 24 | +Input: stones = [3,2,4,1], K = 3 |
| 25 | +Output: -1 |
| 26 | +Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible. |
| 27 | +
|
| 28 | +Example 3: |
| 29 | +
|
| 30 | +Input: stones = [3,5,1,2,6], K = 3 |
| 31 | +Output: 25 |
| 32 | +Explanation: |
| 33 | +We start with [3, 5, 1, 2, 6]. |
| 34 | +We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6]. |
| 35 | +We merge [3, 8, 6] for a cost of 17, and we are left with [17]. |
| 36 | +The total cost was 25, and this is the minimum possible. |
| 37 | +``` |
| 38 | + |
| 39 | +Note: |
| 40 | +1. 1 <= stones.length <= 30 |
| 41 | +2. 2 <= K <= 30 |
| 42 | +3. 1 <= stones[i] <= 100 |
| 43 | + |
| 44 | +### Thinking: |
| 45 | +* Method: dp |
| 46 | + ```Java |
| 47 | + class Solution { |
| 48 | + public int mergeStones(int[] stones, int K) { |
| 49 | + int n = stones.length; |
| 50 | + if((n - 1) % (K - 1) != 0) return -1; |
| 51 | + int[][][] dp = new int[n][n][K + 1]; |
| 52 | + int[] sum = new int[n + 1]; |
| 53 | + for(int i = 0; i < n; i++) |
| 54 | + sum[i + 1] = sum[i] + stones[i]; |
| 55 | + for(int[][] dd: dp) |
| 56 | + for(int[] d: dd) |
| 57 | + Arrays.fill(d, Integer.MAX_VALUE); |
| 58 | + for(int i = 0; i < n; i++) |
| 59 | + dp[i][i][1] = 0; |
| 60 | + for(int l = 2; l <= n; l++){ |
| 61 | + for(int start = 0; start <= n - l; start++){ |
| 62 | + int end = start + l - 1; |
| 63 | + for(int k = 2; k <= K; k++){ |
| 64 | + for(int mid = start; mid < end; mid += K - 1) |
| 65 | + dp[start][end][k] = Math.min(dp[start][end][k], dp[start][mid][1] + dp[mid + 1][end][k - 1]); |
| 66 | + dp[start][end][1] = dp[start][end][K] + sum[end + 1] - sum[start]; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return dp[0][n - 1][1]; |
| 71 | + } |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | +### Reference |
| 76 | +1. [花花酱 LeetCode 1000. Minimum Cost to Merge Stones](https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1000-minimum-cost-to-merge-stones/) |
0 commit comments