本周主题:贪心算法算法
本题难度:Mediumide
作题日期:2017年3月28日学习
本题地址: leetcode.com/problems/mi…spa
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.3d
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.code
Example:cdn
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).blog
题目大意是沿水平直线放 n 个热气球,其大小可用区间(x,y) 表示,热气球能够重叠,而后从 正上方(垂直方向)放箭可刺破气球。问可用最少多少支箭可刺破全部的气球。排序
根据题意,咱们能够抽象成以下的数学问题:给定 n 个区间,求其一共有多少个相交的部分。递归
旁白:只要咱们朝相交区间的任意一点射箭,就能用最少的箭刺破全部的气球。
举个例子:假设有两个气球,其区间范围分别在 [1, 5]、[3, 8] ,那么朝区间[3, 5] 中的任意一点射一支箭就能够刺破两只气球。
这个问题的难点在于:1,输入的 n 个区间都是无序的;2,须要处理 n 个区间。
算法的本质在于下降须要处理的问题的规模。好比动态规划的关键是 求 n 与 1 到 n - 1的关系;递归是须要咱们找出 n 与 n - 1 的关系;二分法是一次将搜索空间折半;分治算法要求咱们将复杂的分词分解成独立的子问题... ...
旁白:这是个人浅显认识,若有不一样的意见,欢迎留言讨论。
根据咱们分析出的难点,咱们能够分两个方面解决该问题。
如图一所示,区间 [0, 13] 和 [2, 5] 合并后,区间大小变成了 [0, 5] 。
旁白:为何是 [0, 5] ?
实际上,在当前的步骤,咱们须要记录两个重要的值: ans 和 terminal 。
ans 表示当前最多须要多少根箭;terminal 表示当前的全部区间的最右边的点。每次合并的本质是肯定 ans 和 terminal 的值,[0, 5] 区间的 5 表示的就是 terminal 的值。
旁白:为何我要用 terminal 而不是 end ?
由于在 某些语言中, end 是一个关键词。
如图二所示,区间[0,8] 和 [2, 15] 合并后,区间大小变成了[0, 8], terminal 的值是 8。
如图三所示,区间[0,8] 和 [9, 15] 合并后, terminal 的值是 15。
根据“题目分析”中的2个区间的三种状态,咱们能够用:
旁白:terminal 的名词太难定义了!其实能够这么理解,terminal 表示的当前这支箭可射区域的最大值。好比在两个相交的区间里,咱们举的第一个例子,[1, 5]、[3, 8] 的重叠区间是 [ 3, 5] ,这说明了在区间 [3,5] 任意一点射箭都是能够的,只是不能过 5 这个点。terminal 就是表示的可射区域的最大值:5。
若是你有更好的解释,麻烦留言评论。谢谢~
具体请参考以下的分析
若是按照 start 升序排列,须要三种状况下 ans 和 terminal 的变化。
旁白:为何只须要考虑不相交的状况?
由于end升序排列,不可能出现嵌套的状况,相交的状况又不要想 ans 和 terminal 的值。
每日一道算法题是由全球3000位小伙伴组成的一个纯粹的算法学习社区:经过天天一块儿作一道算法题来提高咱们的算法能力。
长按下面的二维码,关注每日一道算法题公众号,跟咱们一块儿学习算法!