← Home

452. Minimum Number of Arrows to Burst Balloons

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
452

The trick here is to name the state correctly, then let the implementation follow. For 452. Minimum Number of Arrows to Burst Balloons, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: greedy.

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 findMinArrowShots.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 308 ms, faster than 17.60% of C++ online submissions for Minimum Number of Arrows to Burst Balloons.
02//Memory Usage: 24.6 MB, less than 100.00% of C++ online submissions for Minimum Number of Arrows to Burst Balloons.
03class Solution {
04public:
05    int findMinArrowShots(vector<vector<int>>& points) {
06        if(points.size() == 0) return 0;
07        
08        sort(points.begin(), points.end());
09        
10        int start = points[0][0], end = points[0][1];
11        int ans = 1; //we need at least one arrow
12        
13        for(vector<int>& point : points){
14            // cout << point[0] << " " << point[1] << endl;
15            //Note it's "<=" !
16            if(point[0] <= end){
17                //they have intersection
18                start = point[0];
19                //need to update end!
20                end = min(end, point[1]);
21            }else{
22                ans++;
23                start = point[0];
24                end = point[1];
25            }
26        }
27        
28        return ans;
29    }
30};
31
32//sort by ending point
33//https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/discuss/93703/Share-my-explained-Greedy-solution
34//Runtime: 200 ms, faster than 22.81% of C++ online submissions for Minimum Number of Arrows to Burst Balloons.
35//Memory Usage: 24.8 MB, less than 100.00% of C++ online submissions for Minimum Number of Arrows to Burst Balloons.
36class Solution {
37public:
38    int findMinArrowShots(vector<vector<int>>& points) {
39        if(points.size() == 0) return 0;
40        
41        //sort by ending point
42        sort(points.begin(), points.end(), 
43            [](vector<int>& a, vector<int>& b){
44                return a[1] < b[1];
45            });
46        
47        //shot to the end of first balloon
48        int arrowPos = points[0][1];
49        int arrowCnt = 1;
50        
51        for(vector<int>& point : points){
52            if(point[0] <= arrowPos){
53                //we can use the last arrow to shot this balloon
54                continue;
55            }
56            //we cannot use previous arrow, we need a new one
57            arrowCnt++;
58            arrowPos = point[1];
59        }
60        
61        return arrowCnt;
62    }
63};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
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.