经典的区间选点问题,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; }};新闻热点
疑难解答