← Home

312. Burst Balloons

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
312

This is one of those problems where the clean idea matters more than the amount of code. For 312. Burst Balloons, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming, two pointers.

The notes already sitting in the source point us in the right direction:

  • Divide and conquer and memorization
  • https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are burst, maxCoins.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n^3)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Divide and conquer and memorization
02//https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations
03//Runtime: 28 ms, faster than 27.14% of C++ online submissions for Burst Balloons.
04//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Burst Balloons.
05class Solution {
06public:
07    int burst(vector<vector<int>>& memo, vector<int>& nums, int left, int right){
08        //we can only burst balloons in the range [left+1, right-1], which is empty
09        if(left + 1 == right) return 0;
10        if(memo[left][right] > 0) return memo[left][right];
11        int ans = 0;
12        for(int i = left+1; i <= right-1; i++){
13            //balloon i is the last to be burst, so its neighbors are left and right
14            ans = max(ans, 
15                        nums[i]*nums[left]*nums[right] + 
16                        burst(memo, nums, left, i) + 
17                        burst(memo, nums, i, right)
18                     );
19        }
20        memo[left][right] = ans;
21        return memo[left][right];
22    };
23    
24    int maxCoins(vector<int>& nums) {
25        nums.insert(nums.begin(), 1);
26        nums.push_back(1);
27        int N = nums.size();
28        
29        vector<vector<int>> memo(N, vector<int>(N, 0));
30        
31        //we can burst balloons in the range (0,N-1) = [1,N-2]
32        return burst(memo, nums, 0, N-1);
33    }
34};
35
36//DP
37//https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations
38//Runtime: 16 ms, faster than 89.04% of C++ online submissions for Burst Balloons.
39//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Burst Balloons.
40//time: O(n^3)
41class Solution {
42public:
43    int maxCoins(vector<int>& nums) {
44        nums.insert(nums.begin(), 1);
45        nums.push_back(1);
46        int N = nums.size();
47        
48        vector<vector<int>> dp(N, vector<int>(N, 0));
49        
50        //width: the valid width of range of balloons able to be burst
51        for(int width = 1; width <= N-2; width++){
52            for(int left = 0; left + width + 1 < N; left++){
53                //left, right: the outer element of valid range
54                int right = left + width + 1;
55                //split is the position of last balloon to burst
56                //its valid range is (left, right)
57                for(int split = left+1; split <= right-1; split++){
58                    dp[left][right] = max(dp[left][right],
59                        nums[left]*nums[split]*nums[right] + 
60                        dp[left][split] +
61                        dp[split][right]);
62                }
63            }
64        }
65        
66        return dp[0][N-1];
67    }
68};

Cost

Complexity

Time
O(n^3)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.