20.11.23 leetcode452

题目链接:https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/

题意:给多个区间,求最少需要多少点,可以使得每个区间内都包含至少一个点。

分析:按照右区间从小到大排序,每次都将点放在最小的右区间上。

class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if(points.empty()){
            return 0;
        }
        sort(points.begin(),points.end(),[](const vector<int>& u,const vector<int>& v){
            return u[1]<v[1];
        });
        int pos=points[0][1];
        int ans=1;
        for(auto & v : points){
            if(v[0]>pos){
                pos=v[1];
                ans++;
            }
        }
        return ans;
    }
};

 

posted @ 2020-11-23 22:01  清酒令  阅读(48)  评论(4)    收藏  举报