贪心策略:每一箭射的气球越多,箭使用的数量就越少。
按照右边界排序,从左向右遍历,存在非重叠的区域,箭的数量就有加1。
- public int findMinArrowShots(int[][] points) {
- if (points.length == 0) {
- return 0;
- }
-
- Arrays.sort(points, Comparator.comparingInt(x -> x[1]));
-
- // 至少需要一个
- int count = 1;
- // 右坐标
- int pre = points[0][1];
- for (int[] point : points) {
- // 当前左坐标大于上一右坐标,说明不在同一个重叠区域,需要更新下一个重叠区域
- if (point[0] > pre) {
- count++;
- pre = point[1];
- }
- }
-
- return count;
- }
时间复杂度: