Skip to content

Commit b3d1baa

Browse files
authored
Create 2218-maximum-value-of-k-coins-from-piles.kt
1 parent 2630a41 commit b3d1baa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
fun maxValueOfCoins(piles: List<List<Int>>, k: Int): Int {
3+
val n = piles.size
4+
val dp = Array(n){ IntArray(k+1){-1} }
5+
6+
fun dfs(i: Int, coins: Int): Int {
7+
if (i == n)
8+
return 0
9+
if (dp[i][coins] != -1)
10+
return dp[i][coins]
11+
12+
dp[i][coins] = dfs(i + 1, coins)
13+
14+
var curPile = 0
15+
for (j in 0 until minOf(piles[i].size, coins)) {
16+
curPile += piles[i][j]
17+
dp[i][coins] = maxOf(
18+
dp[i][coins],
19+
curPile + dfs(i + 1, coins - j - 1)
20+
)
21+
}
22+
23+
return dp[i][coins]
24+
}
25+
26+
return dfs(0, k)
27+
}
28+
}

0 commit comments

Comments
 (0)