312. Burst Balloons
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the maximum coins obtainable by bursting a sequence of balloons. The key challenge is that the number of coins from bursting a balloon depends on its adjacent balloons at the moment of bursting. This means the order of bursting is crucial, and a choice made now affects future rewards. For example, bursting a low-value balloon might bring two high-value balloons together, leading to a much larger score later. This interdependency suggests that a simple greedy approach will not work. The problem has optimal substructure (the solution to the overall problem depends on solutions to smaller subproblems) and overlapping subproblems (subproblems are solved multiple times), making it a classic candidate for dynamic programming.
Solution
Burst Balloons in Python
class Solution:
def maxCoins(self, nums: List[int]) -> int:
n = len(nums)
# Create a new list `points` with virtual balloons (value 1) at both ends.
points = [1] + nums + [1]
m = n + 2
# dp[i][j] will be the maximum coins obtained from bursting balloons
# in the open interval (i, j).
dp = [[0] * m for _ in range(m)]
# Iterate over interval lengths, from 3 to m.
# len is the length of the subarray from points[i] to points[j].
for length in range(3, m + 1):
# Iterate over the start of the interval.
for i in range(m - length + 1):
j = i + length - 1
# Iterate over k, the last balloon to be burst in (i, j).
for k in range(i + 1, j):
# Calculate coins for this choice of k.
current_coins = dp[i][k] + dp[k][j] + points[i] * points[k] * points[j]
# Update dp[i][j] with the maximum coins found so far.
dp[i][j] = max(dp[i][j], current_coins)
# The result for the entire array is in dp[0][m-1].
return dp[0][m - 1]
Burst Balloons in Java
class Solution {
public int maxCoins(int[] nums) {
int n = nums.length;
// Create a new array `points` with virtual balloons (value 1) at both ends.
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 m = n + 2;
// dp[i][j] will be the maximum coins obtained from bursting balloons in the open interval (i, j).
int[][] dp = new int[m][m];
// Iterate over interval lengths, from 3 to m.
// len is the length of the subarray from points[i] to points[j].
for (int len = 3; len <= m; len++) {
// Iterate over the start of the interval.
for (int i = 0; i <= m - len; i++) {
int j = i + len - 1;
// Iterate over k, the last balloon to be burst in (i, j).
for (int k = i + 1; k < j; k++) {
// Calculate coins for this choice of k.
int currentCoins = dp[i][k] + dp[k][j] + points[i] * points[k] * points[j];
// Update dp[i][j] with the maximum coins found so far.
dp[i][j] = Math.max(dp[i][j], currentCoins);
}
}
}
// The result for the entire array is in dp[0][m-1].
return dp[0][m - 1];
}
}
Burst Balloons in C++
class Solution {
public:
int maxCoins(vector<int>& nums) {
int n = nums.size();
// Create a new array `points` with virtual balloons (value 1) at both ends.
vector<int> points(n + 2);
points[0] = 1;
points[n + 1] = 1;
for (int i = 0; i < n; ++i) {
points[i + 1] = nums[i];
}
int m = n + 2;
// dp[i][j] will be the maximum coins obtained from bursting balloons in the open interval (i, j).
vector<vector<int>> dp(m, vector<int>(m, 0));
// Iterate over interval lengths, from 3 to m.
// len is the length of the subarray from points[i] to points[j].
for (int len = 3; len <= m; ++len) {
// Iterate over the start of the interval.
for (int i = 0; i <= m - len; ++i) {
int j = i + len - 1;
// Iterate over k, the last balloon to be burst in (i, j).
for (int k = i + 1; k < j; ++k) {
// Calculate coins for this choice of k.
int current_coins = dp[i][k] + dp[k][j] + points[i] * points[k] * points[j];
// Update dp[i][j] with the maximum coins found so far.
dp[i][j] = max(dp[i][j], current_coins);
}
}
}
// The result for the entire array is in dp[0][m-1].
return dp[0][m - 1];
}
};
Burst Balloons in TypeScript
function maxCoins(nums: number[]): number {
const n = nums.length;
// Create a new array `points` with virtual balloons (value 1) at both ends.
const points: number[] = new Array(n + 2);
points[0] = 1;
points[n + 1] = 1;
for (let i = 0; i < n; i++) {
points[i + 1] = nums[i];
}
const m = n + 2;
// dp[i][j] will be the maximum coins obtained from bursting balloons
// in the open interval (i, j).
const dp: number[][] = Array(m).fill(0).map(() => Array(m).fill(0));
// Iterate over interval lengths, from 3 to m.
// len is the length of the subarray from points[i] to points[j].
for (let len = 3; len <= m; len++) {
// Iterate over the start of the interval.
for (let i = 0; i <= m - len; i++) {
const j = i + len - 1;
// Iterate over k, the last balloon to be burst in (i, j).
for (let k = i + 1; k < j; k++) {
// Calculate coins for this choice of k.
const currentCoins = dp[i][k] + dp[k][j] + points[i] * points[k] * points[j];
// Update dp[i][j] with the maximum coins found so far.
dp[i][j] = Math.max(dp[i][j], currentCoins);
}
}
}
// The result for the entire array is in dp[0][m-1].
return dp[0][m - 1];
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.