312. Burst Balloons
Input:[3,1,5,8]
Output:167
Explanation:
nums = [3,1,5,8] -->[3,5,8] -->[3,8] -->[8] -- >[]
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167Last updated
Was this helpful?
Input:[3,1,5,8]
Output:167
Explanation:
nums = [3,1,5,8] -->[3,5,8] -->[3,8] -->[8] -- >[]
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167Last updated
Was this helpful?
Was this helpful?
class Solution {
public int maxCoins(int[] nums) {
// first busing 0s
int [] nums_pad = new int [nums.length + 2];
int n = 1;
for (int x : nums) if( x > 0) nums_pad[n++] = x;
nums_pad[0] = nums_pad[n++] = 1;
int [][] dp = new int [n][n];
for(int s = 2; s < n; s++){ // s is the length of the sliding window
for(int left = 0 ; left + s < n; left++){
int right = left + s;
for(int i = left + 1; i < right; i++){
dp[left][right] = Math.max(dp[left][right],
dp[left][i] + nums_pad[left] * nums_pad[i] * nums_pad[right] + dp[i][right]
);
}
}
}
return dp[0][n-1];
}
}class Solution {
public:
int maxCoins(vector<int>& nums) {
// first bursting 0s
int nums_pad [nums.size() + 2];
int n = 1;
for (int x : nums) if( x > 0) nums_pad[n++] = x;
nums_pad[0] = nums_pad[n++] = 1;
int dp[n][n] = {};
for(int s = 2; s < n; s++){
for(int left = 0 ; left + s < n; left++){
int right = left + s;
for(int i = left + 1; i < right; i++){
dp[left][right] = max(dp[left][right],
dp[left][i] + nums_pad[left] * nums_pad[i] * nums_pad[right] + dp[i][right]
);
}
}
}
return dp[0][n-1];
}
};class Solution(object):
def maxCoins(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_pad = [1] + [x for x in nums if x > 0] + [1]
n = len(nums_pad)
dp = [[0]* n for _ in range(n)] # a fancy way of building the nxn matrix filled with 0
for s in range(2,n):
for left in range(0, n - s):
right = left + s
for i in range(left + 1, right):
dp[left][right] = max(dp[left][right], dp[left][i] + nums_pad[left] *nums_pad[i]* nums_pad[right] + dp[i][right])
return dp[0][n-1]class Solution {
public int maxCoins(int[] nums) {
// first busing 0s
int [] nums_pad = new int [nums.length + 2];
int n = 1;
for (int x : nums) if(x > 0) nums_pad[n++] = x;
nums_pad[0] = nums_pad[n++] = 1;
int [][] mem = new int [n][n]; // memoization step
return burstBallon(mem, nums_pad, 0 , n -1);
}
public int burstBallon(int[][] mem, int [] nums, int left, int right){
// top down + bottom up fashion
// base case
if (left == right -1) return 0;
if (mem[left][right] > 0) return mem[left][right];
int ans = 0;
// caculation step
for(int i = left + 1; i < right; i++){
ans = Math.max(ans, nums[left] * nums[i] * nums[right] + burstBallon(mem,nums, left, i) + burstBallon(mem,nums, i, right));
}
mem[left][right] = ans; //memoization step
return ans;
}
}