Skip to content

Commit 9a3d787

Browse files
authored
Merge pull request #3723 from drxlx/0312-burst-balloons
Create 0312-burst-balloons.swift
2 parents c135e4c + cc1fc3d commit 9a3d787

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

swift/0312-burst-balloons.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func maxCoins(_ nums: [Int]) -> Int {
3+
var nums = [1] + nums + [1]
4+
var dp = [[Int]: Int]()
5+
6+
func dfs(_ l: Int, _ r: Int) -> Int {
7+
if l > r {
8+
return 0
9+
}
10+
if dp[[l, r]] != nil {
11+
return dp[[l, r]]!
12+
}
13+
14+
dp[[l, r]] = 0
15+
for i in l...r {
16+
var coins = nums[l - 1] * nums[i] * nums[r + 1]
17+
coins += dfs(l, i - 1) + dfs(i + 1, r)
18+
dp[[l, r]] = max(dp[[l, r]]!, coins)
19+
}
20+
21+
return dp[[l, r]]!
22+
}
23+
24+
return dfs(1, nums.count - 2)
25+
}
26+
}

0 commit comments

Comments
 (0)