题意:给定一组气球浮动的坐标范围,找出做少的箭来射穿所有气球
思路:
- 把气球安x[0]从小到大排序
- 然后遍历数组,每次当左边界小于当前右边界,那么更新右边界,continue
- 否则,arrow+1,更新右边界
思想:字符规律
复杂度:时间O(nlgn),空间O(1)
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) {
return 0;
}
Arrays.sort(points, new Comparator<int[]> () {
public int compare(int[] o1, int[] o2) {
return o1[1] >= o2[1] ? 1 : -1;
}
});
int arrows = 1;
int right = points[0][1];
for (int[] index : points) {
if (index[0] <= right) {
right = Math.min(right, index[1]);
continue;
}
arrows++;
right = index[1];
}
return arrows;
}
}
网友评论