-
Notifications
You must be signed in to change notification settings - Fork 886
/
BurstBalloons.swift
41 lines (34 loc) · 1.21 KB
/
BurstBalloons.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Question Link: https://leetcode.com/problems/burst-balloons/
* Primary idea: Dynamic Programming, dp[i][j] represents the max coins from ballon i to j,
* transition function: dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j])
*
* Note: for loops should start by length, not by start
*
* Time Complexity: O(n^3), Space Complexity: O(n)
*
*/
class BurstBalloons {
func maxCoins(_ nums: [Int]) -> Int {
guard nums.count > 0 else {
return 0
}
let n = nums.count, nums = renderNums(nums)
var dp = Array(repeating: Array(repeating: 0, count: nums.count), count: nums.count)
for len in 1...n {
for left in 1...n - len + 1 {
let right = left + len - 1
for k in left...right {
dp[left][right] = max(dp[left][right], nums[left - 1] * nums[k] * nums[right + 1] + dp[left][k - 1] + dp[k + 1][right])
}
}
}
return dp[1][n]
}
private func renderNums(_ nums: [Int]) -> [Int] {
var nums = nums
nums.append(1)
nums.insert(1, at: 0)
return nums
}
}