-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBurstBalloons.java
More file actions
31 lines (30 loc) · 1.17 KB
/
BurstBalloons.java
File metadata and controls
31 lines (30 loc) · 1.17 KB
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
class BurstBalloons {
//DP, O(n^3) time and O(n^2) space
//https://discuss.leetcode.com/topic/30746/share-some-analysis-and-explanations
//Let nums[0] = nums[n-1] = 1;
//dp[i][j] is the max coins of bursting balloons at [i+1...j-1].
//dp[i][j] = max{nums[t]*nums[i]*nums[j] + dp[i][t] + dp[t][j], i < t < j};
public int maxCoins(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int[] newNums = new int[nums.length + 2];
newNums[0] = 1;
newNums[nums.length + 1] = 1;
for (int i = 0; i < nums.length; ++i) {
newNums[i + 1] = nums[i];
}
int[][] dp = new int[nums.length + 2][nums.length + 2];
for (int diff = 2; diff <= nums.length + 1; diff++) {
for (int i = 0; i + diff < newNums.length; ++i) {
int j = i + diff;
dp[i][j] = 0;
for (int t = i + 1; t < j; ++t) {
dp[i][j] = Math.max(dp[i][j],
newNums[t]*newNums[i]*newNums[j] + dp[i][t] + dp[t][j]);
}
}
}
return dp[0][nums.length + 1];
}
}