美文网首页
WEEK#1 Minimum Number of Arrows

WEEK#1 Minimum Number of Arrows

作者: DarkKnightRedoc | 来源:发表于2017-12-26 15:21 被阅读0次
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;
    }
};

相关文章

网友评论

      本文标题:WEEK#1 Minimum Number of Arrows

      本文链接:https://www.haomeiwen.com/subject/zecjgxtx.html