Skip to content

Commit

Permalink
Create 0312-burst-balloons.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Dec 29, 2022
1 parent 324cd74 commit fd2bf77
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kotlin/0312-burst-balloons.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
fun maxCoins(nums: IntArray): Int {
val newNums = intArrayOf(1) + nums + intArrayOf(1)
val cache = Array(newNums.size){ IntArray(newNums.size) }

fun dfs(left: Int, right: Int): Int{
if(left > right) return 0
if(cache[left][right] != 0) return cache[left][right]
for(i in left..right){
var coins = newNums[left-1] * newNums[i] * newNums[right+1]
coins = coins + dfs(left, i-1) + dfs(i+1, right)
cache[left][right] = maxOf(cache[left][right], coins)
}
return cache[left][right]
}

return dfs(1, newNums.size-2)

}

}

0 comments on commit fd2bf77

Please sign in to comment.