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; for (const auto &v : points) { if (v[0] > r) { ++res; r = v[1]; } else { r = min(r, (long)v[1]); } } return res; } };
|