Skip to content

Commit ff3602d

Browse files
committed
Leetcode | Minimum Cost to Merge Stones | Kotlin
1 parent 7beeb54 commit ff3602d

3 files changed

Lines changed: 94 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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.devstromo.hard.p1000
2+
3+
class SolutionKt {
4+
5+
fun mergeStones(stones: IntArray, k: Int): Int {
6+
val n = stones.size
7+
if ((n - 1) % (k - 1) != 0) return -1
8+
9+
val prefixSum = IntArray(n + 1)
10+
for (i in stones.indices) {
11+
prefixSum[i + 1] = prefixSum[i] + stones[i]
12+
}
13+
14+
val dp = Array(n) { Array(n) { IntArray(k + 1) { Int.MAX_VALUE } } }
15+
16+
for (i in 0 until n) {
17+
dp[i][i][1] = 0
18+
}
19+
20+
for (length in 2..n) {
21+
for (i in 0..n - length) {
22+
val j = i + length - 1
23+
for (p in 2..k) {
24+
for (m in i until j step (k - 1)) {
25+
if (dp[i][m][1] != Int.MAX_VALUE && dp[m + 1][j][p - 1] != Int.MAX_VALUE) {
26+
dp[i][j][p] = minOf(dp[i][j][p], dp[i][m][1] + dp[m + 1][j][p - 1])
27+
}
28+
}
29+
}
30+
// Merge into 1 pile
31+
if (dp[i][j][k] != Int.MAX_VALUE) {
32+
dp[i][j][1] = dp[i][j][k] + prefixSum[j + 1] - prefixSum[i]
33+
}
34+
}
35+
}
36+
37+
return dp[0][n - 1][1]
38+
}
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.devstromo.hard.p1000
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Minimum Cost to Merge Stones`() {
11+
assertEquals(20, solution.mergeStones(intArrayOf(3, 2, 4, 1), 2))
12+
assertEquals(-1, solution.mergeStones(intArrayOf(3, 2, 4, 1), 3))
13+
assertEquals(25, solution.mergeStones(intArrayOf(3, 5, 1, 2, 6), 3))
14+
}
15+
}

0 commit comments

Comments
 (0)