Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1700 from a93a/312
Browse files Browse the repository at this point in the history
Create 0312-burst-balloons.kt
  • Loading branch information
Ahmad-A0 authored Dec 29, 2022
2 parents 4aa1e1d + fd2bf77 commit f3c2e5c
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 f3c2e5c

Please sign in to comment.