Minimum Number of Arrows to Burst Balloons

題目來(lái)源
這道題感覺不難,先排個(gè)序,然后從左向右掃描,保存掃描過(guò)的氣球最大左邊界和最小右邊界,當(dāng)最大左邊界小于最小右邊界時(shí),需要射一箭。

class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& points) {
        sort(points.begin(), points.end());
        int n = points.size(), res = 0;
        for (int i=0; i<n; i++) {
            int left_max = points[i].first, right_min = points[i].second;
            while (left_max <= right_min) {
                i++;
                left_max = max(left_max, points[i].first);
                right_min = min(right_min, points[i].second);
            }
            i--;
            res++;
        }
        return res;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容