0%

452. Minimum Number of Arrows to Burst Balloons

O(nlogn + n)
问贴海报需要几根钉子,扫描线做不了
贪心,先排序,只要左端升序即可,右端无所谓,因为始终维护右端最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if (points.empty()) return 0;
sort(begin(points), end(points));
int res = 0;
long r = LONG_MIN; // 必须要用long,反例[INT_MIN, INT_MAX]
for (const auto &v : points) {
if (v[0] > r) {
++res;
r = v[1]; // 因为要开启一个新的区间,所以直接使用这个区间的最右端
} else {
r = min(r, (long)v[1]);
}
}
return res;
}
};