452. Minimum Number of Arrows to Burst Balloons

https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/

给定一堆区间,从每个区间选一个数,问最少需要多少数能代表所有的区间。间问题先排序,从排在前面的区间选能一个元素能尽可能能多覆盖其它区间,那只能是end元素,覆盖了所有与该区间所有有交集的区间。把重叠区间跳过后依次类推。

/*
 * @lc app=leetcode id=452 lang=cpp
 *
 * [452] Minimum Number of Arrows to Burst Balloons
 */

// @lc code=start
class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.empty()) return 0;
        sort(points.begin(), points.end(), [](const auto &a, const auto &b){
            return a[1] < b[1];
        });
        int res = 1, end = points[0][1];
        for (const auto &p : points) {
            if (p[0] <= end) continue;
            ++res;
            end = p[1];
        }
        return res;
    }
};
// @lc code=end

Last updated