452. Minimum Number of Arrows to Burst Balloons

https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 10**4 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

1
2
3
4
5
6
7
8
9
10
11
Example:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and
[1,6]) and another arrow at x = 11 (bursting the other two balloons).

java


https://discuss.leetcode.com/topic/66579/java-greedy-soution

Java Greedy Soution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int findMinArrowShots(int[][] points) {
if(points==null || points.length==0 || points[0].length==0) return 0;
Arrays.sort(points, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0]==b[0]) return a[1]-b[1];
else return a[0]-b[0];
}
});

int minArrows = 1;
int arrowLimit = points[0][1];
for(int i=1;i<points.length;i++) {
int[] baloon = points[i];
if(baloon[0]<=arrowLimit) {
arrowLimit=Math.min(arrowLimit, baloon[1]);
} else {
minArrows++;
arrowLimit=baloon[1];
}
}
return minArrows;
}

https://discuss.leetcode.com/topic/66548/concise-java-solution-tracking-the-end-of-overlapping-intervals

Concise Java solution tracking the end of overlapping intervals

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public int findMinArrowShots(int[][] points) {
if(points == null || points.length < 1) return 0;
Arrays.sort(points, (a, b)->(a[0]-b[0]));
int result = 1;
int end = points[0][1];
for(int i = 1; i < points.length; i ++) {
if(points[i][0] > end) {
result ++;
end = points[i][1];
} else {
end = Math.min(end, points[i][1]);
}
}
return result;
}
}

https://discuss.leetcode.com/topic/70330/java-easy-to-understand-solution

Java easy to understand solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
public int findMinArrowShots(int[][] points) {
if (points == null || points.length == 0) return 0;

Arrays.sort(points,(a, b) -> a[0] - b[0]); //sort the balloons according to their start coordinate

int minRight = Integer.MAX_VALUE, count = 0;
//minRight record the leftmost end of previous balloons
for (int i = 0; i < points.length; ++i) {
//whenever current balloon's start is bigger than minRight
//that means we need an arrow to clear all previous balloons
if (points[i][0] > minRight) {
count++;
minRight = points[i][1];
} else {
minRight = Math.min(minRight, points[i][1]);
}
}
return count + 1;
}
}

cpp


https://discuss.leetcode.com/topic/66709/c-easy-understood-solution-sort

C++ easy understood solution (sort)

First, we sort balloons by increasing points.end (if ends are the same, then by increasing of points.start). Every time arrow shot points.end, say, points[i].second. If next balloon.start <= points[i].second, it is also shot, thus we continue.

1
2
3
4
5
6
7
8
9
10
11
12
13
int findMinArrowShots(vector<pair<int, int>>& points) {
int count = 0, arrow = INT_MIN;
sort(points.begin(), points.end(), mysort);
for(int i = 0; i<points.size(); i++){
if(arrow!=INT_MIN && points[i].first<=arrow){continue;} //former arrow shot points[i]
arrow = points[i].second; // new arrow shot the end of points[i]
count++;
}
return count;
}
static bool mysort(pair<int, int>& a, pair<int, int>& b){
return a.second==b.second?a.first<b.first:a.second<b.second;
}

https://discuss.leetcode.com/topic/66539/c-o-nlogn-solution-with-detailed-explanation

C++ O(nlogn) solution with detailed explanation

The main idea here sort the array first, and then find overlapping sequence which should cover the min(points[i].second, …, points[k].second), the operation above is insure to pick the common region from points i to points k. Time complexity is O(nlogn) for sort, and space complexity is O(n).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int findMinArrowShots(vector<pair<int, int>>& points) {
if(points.empty()) return 0;
int ans = 0;
sort(points.begin(), points.end(), cmp);
for(int i = 0; i < points.size(); ++ i) {
int j = i + 1;
while(j < points.size() && points[j].first <= points[i].second) {
points[i].second = min(points[i].second, points[j].second);
j ++;
}
ans += 1;
i = j - 1;
}

return ans;
}

static bool cmp(pair<int, int>& a, pair<int, int>& b) {
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
};

python


https://discuss.leetcode.com/topic/66772/greedy-python-132-ms

Greedy, Python (132 ms)

  1. Sort intervals by ending value;
  2. Only count valid intervals we need, and skip overlapping intervals

return the count

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
points = sorted(points, key = lambda x: x[1])
res, end = 0, -float('inf')
for interval in points:
if interval[0] > end:
res += 1
end = interval[1]
return res
谢谢你,可爱的朋友。