An easy training of greedy algorithm. We just need to sort the vector of pairs to an ascending order on the basis of its second value, said the right bound of each balloon, making it the position of the arrow. Then comparisons between every left bound value and the arrow are made, in order to judge whether should we increase the count.
class Solution {
public:
static bool Comp(pair<int,int> p1, pair<int,int> p2){
return p1.second<p2.second;
}
int findMinArrowShots(vector<pair<int, int>>& points) {
int arrow = INT_MIN;
int count = 0;
sort(points.begin(),points.end(),Comp);
for(int i=0;i<points.size();i++){
if(arrow!=INT_MIN && points[i].first<=arrow)
continue;
else{
arrow = points[i].second;
count++;
}
}
return count;
}
};