# Force deduction note: 452. Detonate the balloon with the least number of arrows

```452. Detonate the balloon with the least number of arrows
There are many spherical balloons in two-dimensional space. For each balloon, the input provided is the start and end coordinates of the balloon diameter in the horizontal direction. Because it is horizontal, the ordinate is not important, so it is enough to know the abscissa of the beginning and end. The start coordinate is always less than the end coordinate.

A bow and arrow can follow x The axis shoots out completely perpendicular from different points. In coordinates x Shoot an arrow at, if there is a balloon, the starting and ending coordinates of the diameter are xstart，xend， And meet  xstart ≤ x ≤ xend，The balloon will be detonated. There is no limit to the number of bows and arrows you can shoot. Once a bow and arrow is shot, it can move forward indefinitely. We want to find the minimum number of bows and arrows needed to detonate all balloons.

Give you an array points ，among points [i] = [xstart,xend] ，Returns the minimum number of bows and arrows that must be fired to detonate all balloons.

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: for this example, x = 6 It can shoot and explode [2,8],[1,6] Two balloons, and x = 11 Shoot the other two balloons
Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Example 4:

Input: points = [[1,2]]
Output: 1
Example 5:

Input: points = [[2,3],[2,3]]
Output: 1

Tips:

1 <= points.length <= 104
points[i].length == 2
-231 <= xstart < xend <= 231 - 1
```

https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/solution/yong-zui-shao-shu-liang-de-jian-yin-bao-qi-qiu-1-2/

The general meaning is to let each arrow pierce more balloons as much as possible (nonsense)
In order to achieve this goal, we need to find more balloons with overlapping States, find the balloon with the most overlapping States, break it, and then deal with the remaining balloons with suboptimal overlapping States, and so on.

There are two ways to turn ideas into code:
1. Sort the balloons according to the left value. When the left value does not exceed the previous balloon's right value, the balloons overlap (because the default left value after sorting is less than the previous balloon). Note that the right value is the right value, and we must keep recording the status of the smallest right value.
Why? (this problem is also related to the second method and what puzzles me). Consider {2,8} {3,4} {6,10}. After drawing, I find that it takes two arrows to solve this problem. If we only consider the left value, we will misjudge that only one arrow is needed. Therefore, we can find that when the left value of the balloon is greater than the minimum right value of the previous balloon, even if the left value is within the overlapping range of the previous balloon, there is no way to solve all balloons with one arrow. Therefore, when the left value is greater than the minimum right value, you need to add an arrow

2. (see official answers for details)

What bothers me here is also very important:
There was a timeout error in the previous submission. Later, it was found that the reason was the cmp function
The formal parameters must use the method of reference assignment. At the beginning, I used the method of direct assignment, that is, I didn't add reference symbols. The reason for the timeout should be that the number of original arrays is large and it is a two-dimensional array. When the quick sort continuously calls cmp, the time overhead is large

```class Solution {
public:
static bool cmp(vector<int> &A,vector<int> &B){
return A < B;
}
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(),points.end(),cmp);
int left = points;
int right = points;
int ans = 1;
for(int i = 1;i < points.size();++i){
if(points[i] > right){
ans++;
left = points[i];
right = points[i];
}
else{
right = min(right,points[i]);
}
}
return ans;
}
};
```

```class Solution {
public:
static bool cmp(vector<int> &A,vector<int> &B){
return A < B;
}
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(),points.end(),cmp);
int right = points;
int ans = 1;
for(int i = 1;i < points.size();++i){
if(points[i] > right){
ans++;
right = points[i];
}
}
return ans;
}
};
```

Tags: C++ Algorithm leetcode

Posted on Sat, 30 Oct 2021 10:28:06 -0400 by sunilvadranapu