Leetcode 452. Minimum Number of Arrows to Burst Balloons 贪心算法,注意边界条件,sort函数自定义比较运算符

class Solution {
public:
    static bool cmp(const vector<int>& v1, const vector<int>& v2){
        return v1[1]<v2[1];
    }
    int findMinArrowShots(vector<vector<int>>& points) {
        if(points.size()==0) return 0;
        sort(points.begin(), points.end(), cmp);
        int end_time = points[0][1];
        int count=1;
        for(auto& event: points){
            if(event[0] > end_time){
                count++;
                end_time=event[1];
            }
        }
        return count;
    }
};

你可能感兴趣的:(算法)