[抄题]:

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 xendbursts 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).

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

二维数组就写个points.len = 0 就行了,没必要写points[0].len = 0

[思维问题]:

知道是扫描线,忘了怎么写了:更新结尾。必要时+count

[英文数据结构或算法,为什么不用别的数据结构或算法]:

扫描线要先对取件进行排序。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

为了一把箭能涉及到全部,end选取的是min,需要因地制宜

[一刷]:

  1. 循环过程中要依据end来进行更新

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

以后强制性写test case了:

/*
Input:
[[10,16], [2,8], [1,6], [7,12]]
sort:
Input:
[[1,6], [2,8], [7,12], [10,16]]
end 6 6 12 12
count 1 1 +1=2 2
*/

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int findMinArrowShots(int[][] points) {
//corner cases
if (points == null || points.length == 0) return 0; //initialization: sort
int count = 1;
Arrays.sort(points, (a, b) -> (a[0] - b[0])); //for loop and get count
int end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (end < points[i][0]) {
count++;
end = points[i][1];
}
else end = Math.min(end, points[i][1]);
} /*
Input:
[[10,16], [2,8], [1,6], [7,12]]
sort:
Input:
[[1,6], [2,8], [7,12], [10,16]]
end 6 6 12 12
count 1 1 +1=2 2
*/ //return
return count;
}
}

452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少的更多相关文章

  1. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  2. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  3. [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 ...

  4. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  5. 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. [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 ...

  7. [LC] 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. 【leetcode】452. Minimum Number of Arrows to Burst Balloons

    题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...

  9. 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球

    在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...

随机推荐

  1. hsdfz -- 6.17 -- day2

    今日依旧康复…… 当天晚上被老师拉去小吃街了,晚上回来精力憔悴,所以并没有当天写 反正就惨,因为估错复杂度,期望得分100分最后结果20分 (我的复杂度是nlog^2n的,正确性有保障,稳! 事后:还 ...

  2. PythonStudy——Python 内置函数 Built-in function

    内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...

  3. PythonStudy——内存管理之垃圾回收 Memory management Garbage collection

    内存管理 引用计数:垃圾回收机制的依据 # 1.变量的值被引用,该值的引用计数 +1# 2.变量的值被解绑,该值的引用计数 -1# 3.引用计数为0时就会被垃圾回收机制回收 标记清除:解决循环引用问题 ...

  4. PythonStudy——算术运算符 Arithmetic operator

    # 减法 # 加法 print(10 + 20) print('abc' + 'def') print([1, 2, 3] + [4, 5, 6]) Output: 30  abcdef  [1, 2 ...

  5. 深入理解CSS系列(一):理解CSS的盒子模型

    接触前端也有好几个年头了,但是,讲实话,对于CSS的理解真的是不敢恭维,相信很多同行也有类似的感受吧!这是为什么呢?因为我们都认为CSS太简单了,没有必要深入学习,果真如此?其实,只不过是自己图样图森 ...

  6. docker之 网络模式和跨主机通信

    Docker的四种网络模式Bridge模式 当Docker进程启动时,会在主机上创建一个名为docker0... Docker的四种网络模式 Bridge模式 当Docker进程启动时,会在主机上创建 ...

  7. 半分钟学会使用markdown基本语法

    想看文字版的看这个吧https://www.jianshu.com/p/191d1e21f7ed

  8. minicom 抓取log

    使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...

  9. html/css/js-如何利用jq来更改属性的值和获取属性的值

    jquery的使用在web开发中是非常广泛的,虽然说比较容易,易学,但在开发过程中,也总是会碰到各种各样的小问题. 我曾经就遇到这种问题,jq如何获取属性值和更改属性值的. 众所周知,attr()可以 ...

  10. 04-体验一下apache组织封装的BeanUtil工具包

    apache 自己为程序员们封装了一个专门用于处理的工具类,其功能有(数据类型会自动转成与JavaBean相关的) map转javabean javabean转map javabean对象复制 获取j ...