Src: https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/
在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了。开始坐标总是小于结束坐标。
一支弓箭可以沿着 x 轴从不同点完全垂直地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被引爆。可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。
给你一个数组 points ,其中 points [i] = [xstart,xend] ,返回引爆所有气球所必须射出的最小弓箭数。
执行用时: 18 ms 内存消耗: 46.1 MB
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) {
return 0;
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
//按右边界大小升序排列
public int compare(int[] o1, int[] o2) {
int a = o1[1], b = o2[1];
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
//return o1[1] - o2[1];
//Intmax-IntMin会越界
}
});
int ans = 0;
int pos = points[0][1];
ans++;
for (int[] bolloon : points) {
if (bolloon[0] > pos) {
pos = bolloon[1];
ans++;
}
}
return ans;
}
}