WEEK#1 Minimum Number of Arrows to Burst Balloons

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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容