WEEK#1 Minimum Number of Arrows

2017-12-26  本文已影响0人  DarkKnightRedoc
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<int,int> p1, pair<int,int> p2) {
    return p1.second < p2.second || (p1.second == p2.second && p1.first < p2.first);
}

class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& 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;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读