forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0312-burst-balloons.java
38 lines (30 loc) · 974 Bytes
/
0312-burst-balloons.java
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
// Solution: Dynamic Programming with Memoization
// Time Complexity: O(n^3)
// Extra Space Complexity: O(n^2)
class Solution {
public int maxCoins(int[] nums) {
var dp = new int[nums.length][nums.length];
return burst(nums, 0, nums.length - 1, dp);
}
private int burst(int[] nums, int left, int right, int[][] dp) {
if (left > right) {
return 0;
}
if (dp[left][right] != 0) {
return dp[left][right];
}
for (int i = left; i <= right; i++) {
int coins = nums[i];
if (left - 1 >= 0) {
coins *= nums[left - 1];
}
if (right + 1 < nums.length) {
coins *= nums[right + 1];
}
coins +=
burst(nums, left, i - 1, dp) + burst(nums, i + 1, right, dp);
dp[left][right] = Math.max(dp[left][right], coins);
}
return dp[left][right];
}
}