Skip to content

Commit fd2bf77

Browse files
committed
Create 0312-burst-balloons.kt
1 parent 324cd74 commit fd2bf77

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kotlin/0312-burst-balloons.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
fun maxCoins(nums: IntArray): Int {
3+
val newNums = intArrayOf(1) + nums + intArrayOf(1)
4+
val cache = Array(newNums.size){ IntArray(newNums.size) }
5+
6+
fun dfs(left: Int, right: Int): Int{
7+
if(left > right) return 0
8+
if(cache[left][right] != 0) return cache[left][right]
9+
for(i in left..right){
10+
var coins = newNums[left-1] * newNums[i] * newNums[right+1]
11+
coins = coins + dfs(left, i-1) + dfs(i+1, right)
12+
cache[left][right] = maxOf(cache[left][right], coins)
13+
}
14+
return cache[left][right]
15+
}
16+
17+
return dfs(1, newNums.size-2)
18+
19+
}
20+
21+
}

0 commit comments

Comments
 (0)