Leetcode 452 .Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
题意就是给出一些气球,想办法用最少的次数刺破这些气球。他们的位置用x轴上的一个区间表示,如果某些气球的区间存在交集,则可以同时刺破这些气球。

可以利用贪心算法的思想。一开始我想出了一种方法,最后有很多样例过不了,仔细想想是不完备的算法。就是每次选一个气球,然后对后面的气球不断取交集,来抵消其他的气球。仔细想可以发现这样并不一定保证得到最优解。

class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& points) {
        int left, right;
        int count = 0;
        for (int i = 0; i < points.size(); i++) {
            if (points[i].first != 0 && points[i].second != 0) {
                count++;
                left = points[i].first;
                right = points[i].second;
            }
            else {
                continue;
            }
            for (int j = 1; j < points.size(); j++) {


                if (points[j].first >= left&&points[j].second <= right) {
                    left = points[j].first;
                    right = points[j].second;
                    points[j].first = 0;
                    points[j].second = 0;
                }
                else if (points[j].first <= left&&points[j].second >= right) {
                    // no change
                    points[j].first = 0;
                    points[j].second = 0;
                }
                else if (points[j].first <= left&&points[j].second <= right&&points[j].second>=left) {
                    right = points[j].second;
                    points[j].first = 0;
                    points[j].second = 0;
                }
                else if (points[j].first >= left&&points[j].first<=right&&points[j].second >= right) {
                    left = points[j].first;
                    points[j].first = 0;
                    points[j].second = 0;
                }
            }
        }
        return count;
    }
};

标准的答案很好理解,就是按照这些气球的区间的结束位置从小到大先来一次排序,然后每次都刺向这些区间的结束位置。保证消灭最多的气球。时间复杂度o(nlogn)

class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& points) {
        if ( points.size() == 0) {
            return 0;
        }
        if (points.size() == 1) {
            return 1;
        }
        sort(points.begin(), points.end(), cmp);
        int end=points[0].second;
        int count = 1;
        for (int i = 1; i < points.size(); i++) {
            if (points[i].first <=end) {
                continue;
            }
            else {
                count++;
                end = points[i].second;
            }
        }
        return count;
    }
    static bool cmp(pair<int, int> & a, pair<int, int>& b) {
        if (a.second <= b.second) {
            return true;
        }
        else {
            return false;
        }
    }
};

猜你喜欢

转载自blog.csdn.net/hello_my_coder/article/details/78915822