Skip to content

Commit d30f6e9

Browse files
committed
Leetcode | Minimum Cost to Merge Stones | Java
1 parent 72484ab commit d30f6e9

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 1000. Minimum Cost to Merge Stones
2+
3+
There are `n` piles of `stones` arranged in a row. The `ith` pile has `stones[i]` stones.
4+
5+
A move consists of merging exactly `k` consecutive piles into one pile, and the cost of this move is equal to the total
6+
number of stones in these `k` piles.
7+
8+
Return the minimum cost to merge all piles of stones into one pile. If it is impossible, return `-1`.
9+
10+
Example 1:
11+
12+
Input: stones = [3,2,4,1], k = 2
13+
Output: 20
14+
Explanation: We start with [3, 2, 4, 1].
15+
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
16+
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
17+
We merge [5, 5] for a cost of 10, and we are left with [10].
18+
The total cost was 20, and this is the minimum possible.
19+
20+
Example 2:
21+
22+
Input: stones = [3,2,4,1], k = 3
23+
Output: -1
24+
Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.
25+
26+
Example 3:
27+
28+
Input: stones = [3,5,1,2,6], k = 3
29+
Output: 25
30+
Explanation: We start with [3, 5, 1, 2, 6].
31+
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
32+
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
33+
The total cost was 25, and this is the minimum possible.
34+
35+
Constraints:
36+
37+
- `n == stones.length`
38+
- `1 <= n <= 30`
39+
- `1 <= stones[i] <= 100`
40+
- `2 <= k <= 30`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.devstromo.hard.p1000;
2+
3+
public class Solution {
4+
public int mergeStones(int[] stones, int k) {
5+
int n = stones.length;
6+
if ((n - 1) % (k - 1) != 0) return -1;
7+
8+
int[][][] dp = new int[n][n][k + 1];
9+
int[] prefix = new int[n + 1];
10+
11+
for (int i = 0; i < n; i++)
12+
prefix[i + 1] = prefix[i] + stones[i];
13+
14+
for (int len = 2; len <= n; len++) {
15+
for (int i = 0; i + len <= n; i++) {
16+
int j = i + len - 1;
17+
for (int p = 2; p <= k; p++) {
18+
dp[i][j][p] = Integer.MAX_VALUE;
19+
for (int m = i; m < j; m += (k - 1)) {
20+
if (dp[i][m][1] == Integer.MAX_VALUE || dp[m + 1][j][p - 1] == Integer.MAX_VALUE)
21+
continue;
22+
dp[i][j][p] = Math.min(dp[i][j][p], dp[i][m][1] + dp[m + 1][j][p - 1]);
23+
}
24+
}
25+
26+
// Merge into 1 pile
27+
dp[i][j][1] = (dp[i][j][k] == Integer.MAX_VALUE) ? Integer.MAX_VALUE : dp[i][j][k] + prefix[j + 1] - prefix[i];
28+
}
29+
}
30+
31+
return dp[0][n - 1][1];
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.devstromo.hard.p1000;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Minimum Cost to Merge Stones")
13+
void testMinimumCostToMergeStones() {
14+
assertEquals(20, solution.mergeStones(new int[]{3, 2, 4, 1}, 2));
15+
assertEquals(-1, solution.mergeStones(new int[]{3, 2, 4, 1}, 3));
16+
assertEquals(25, solution.mergeStones(new int[]{3, 5, 1, 2, 6}, 3));
17+
}
18+
19+
}

0 commit comments

Comments
 (0)