-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path312.burst-balloons.java
More file actions
33 lines (29 loc) · 928 Bytes
/
312.burst-balloons.java
File metadata and controls
33 lines (29 loc) · 928 Bytes
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
/*
* @lc app=leetcode id=312 lang=java
*
* [312] Burst Balloons
*/
// @lc code=start
class Solution {
public int maxCoins(int[] nums) {
int n = nums.length;
int[] points = new int[n + 2];
points[0] = 1;
points[n + 1] = 1;
for (int i = 0; i < n; i++) {
points[i + 1] = nums[i];
}
int[][] dp = new int[n + 2][n + 2];
//dp[i][j] represent the points we can get between i and j excluede ij.
for (int i = n; i >= 0; i--) {
for (int j = i + 1; j < n + 2; j++) {
for (int k = i + 1; k < j; k++) {
dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + points[i] * points[k] * points[j]);
}
}
}
return dp[0][n + 1];
}
}
//https://leetcode-cn.com/problems/burst-balloons/solution/dong-tai-gui-hua-tao-lu-jie-jue-chuo-qi-qiu-wen-ti/
// @lc code=end