Leetcode-452Minimum Number of Arrows to Burst Balloons

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轴的位置向y轴方向射箭,给定气球的宽度为 x :begin <= x <= end,如果想要打爆所有的气球,至少需要多少弓箭手?
当输入[[10,16], [2,8], [1,6], [7,12]]时:
如图:


Leetcode-452Minimum Number of Arrows to Burst Balloons_第1张图片
image.png

至少需要两个弓箭手才能够把所有的气球打爆;输出2。
下面我们来分析一下这个问题:
输入获得气球的(begin,end) ,我们首先将这些气球按照begin的值从小到大进行排列:
[[10,16], [2,8], [1,6], [7,12]] 变为 [[1,6], [2,8], [7,12], [10,16]] ;

  1. 当第二个气球 [2,8] 中的 begin 小于第一个气球 [1,6] 中的end 时,说明弓箭手可以同时穿过[1,6],[2,8]两个气球;
    我们能够发现,穿过[1,6],[2,8]两个气球的区域范围为[2,6];为了射出的箭能够尽可能的穿过最多的气球,最佳的策略是让弓箭手在x = 6 的位置射箭,也就是说,要让 end 的值取所能取到的最大值;
  2. 第三个气球 [7,12] 中的 begin 小于区域[2,6] 中的end,所以气球1,2,3不可能同时被穿过,只能增加一个弓箭手,而这个弓箭手现在所能覆盖的区域为[7,12];
  3. 第四个气球 [10,16] 中的 begin 小于区域[7,12] 中的end,所以气球3,4可以同时被穿过,此时弓箭手所能覆盖的射箭区域为[10,12],最佳的射箭位置为x = end = 12 的位置;
    最后,我们考虑下特殊情况:
    当不存在气球的时候,输入为空,此时,我们直接返回0。

My Solution(C/C++完整实现):

#include 
#include 
#include 
#include 

using namespace std;

//bool cmp(pair &p1, pair &p2) {
//  return p1.first < p2.first;
//}

class Solution {
public:
    int findMinArrowShots(vector> &points) {
        //sort(points.begin(), points.end(),cmp);
        sort(points.begin(), points.end());
        int begin = points[0].first;
        int end = points[0].second;
        int arrowShots = 1;
        for (int i = 1; i < points.size(); i++) {
            if (points[i].first <= end) {
                begin = points[i].first;
                if (points[i].second < end) {
                    end = points[i].second;
                }
            }
            else {
                arrowShots += 1;
                begin = points[i].first;
                end = points[i].second;
            }
        }
        return arrowShots;
    }
};

int main() {
    vector> p;
    p.push_back(make_pair(10, 16));
    p.push_back(make_pair(2, 8));
    p.push_back(make_pair(1, 6));
    p.push_back(make_pair(7, 12));
    Solution s;
    printf("arrowShots: %d ", s.findMinArrowShots(p));
    return 0;
}

结果:

arrowShots: 2

你可能感兴趣的:(Leetcode-452Minimum Number of Arrows to Burst Balloons)