https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
这一题让我想起了另一题,如果在一群schedule里面得到所需的最少的meeting room。https://blog.csdn.net/chaochen1407/article/details/81282319
那一题本质就是找最大的overlap的层数有多少。而这一题,也有利用到overlap这个概念,只是多少还有点不一样。
这一题是找overlap的同时应用了贪心算法。先用起点进行排序,然后不停往下遍历,同时再不停缩小overlap的面积(缩小的方式就是看Xend,永远取更小的Xend),如果下一个点的起点在这个overlap的面积外,计数器就加一,然后重置新的overlap的面积为新的Xstart, Xend。如此往返到最后。根据这个算法,得到代码如下:
public int findMinArrowShots(int[][] points) {
if (points.length == 0) return 0;
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int counter = 1;
int head = points[0][0], tail = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > tail) {
counter++;
head = points[i][0];
tail = points[i][1];
} else {
tail = Math.min(points[i][1], tail);
}
}
return counter;
}