WEEK#1 Minimum Number of Arrows to Burst Balloons

WEEK#1 Minimum Number of Arrows to Burst Balloons_第1张图片
Problem Description
  1. Sort the bullons by their ending points
  2. Each time we shoot an arrow aiming at the smallest ending point and remove all the bullons that are bursted by this arrow
  3. Continue to do step 2 until all bullons are bursted
bool mycompare(pair p1, pair p2) {
    return p1.second < p2.second || (p1.second == p2.second && p1.first < p2.first);
}

class Solution {
public:
    int findMinArrowShots(vector>& points) {
        sort(points.begin(), points.end(), mycompare);
        
        int Count = 0;
        while (!points.empty()) {
            int CurrentArrow = points.front().second;
            //cout << "CurrentArrow = " << CurrentArrow << endl;
            Count++;
            for (int i = 0; i < points.size(); i++) {
                if (CurrentArrow >= points[i].first && CurrentArrow <= points[i].second) {
                    //cout << "i = " << i << endl;
                    //cout << "points[i].first = " << points[i].first << " points[i].second = " << points[i].second << endl;
                    points.erase(points.begin() + i);
                    i = -1;
                }
            }
        }
        return Count;
    }
};

你可能感兴趣的:(WEEK#1 Minimum Number of Arrows to Burst Balloons)