452. Minimum Number of Arrows to Burst Balloons

来源:互联网 发布:小篆转换器软件下载 编辑:程序博客网 时间:2024/06/05 18:08

经典的区间选点问题,greedy策略

class Solution {public:    static bool cmp(pair<int, int>& a,pair<int,int>& b)    {        if(a.second<b.second)            return true;        else if(a.second==b.second&&a.first<b.first)            return true;        else            return false;    }    int findMinArrowShots(vector<pair<int, int>>& points) {        if(points.size()==0)            return 0;        sort(points.begin(),points.end(),cmp);        int now=0;        int next=1;        int count=1;        while(next<points.size())        {            if(points[now].second>=points[next].first)                next++;            else            {                count++;                now=next;                next++;            }        }        return count;    }};
0 0
原创粉丝点击