[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 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).
class Solution {
public int findMinArrowShots(int[][] points) {
if (points == null || points.length == 0) {
return 0;
}
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int res = 1;
int end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > end) {
res += 1;
end = points[i][1];
} else {
end = Math.min(end, points[i][1]);
}
}
return res;
}
}
[LC] 452. Minimum Number of Arrows to Burst Balloons的更多相关文章
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- [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 ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [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 ...
- 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球
在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
随机推荐
- 关于mybatis的<selectKey>中的keyColumn
<mapper namespace="com.dao.EmployeeDao"> <insert id="insert"> <se ...
- Caused by: com.mysql.cj.exceptions.DataReadException: Zero date value prohibited
原因:数据库日期出现零值,即0000-00-00 属于一个无效日期. 解决方案:重新赋值,或者在jdbc链接后加参数zeroDateTimeBehavior=convertToNull
- PAT 2019 秋
考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...
- UVA - 10934 Dropping water balloons(装满水的气球)(dp)
题意:有k个气球,n层楼,求出至少需要多少次实验能确定气球的硬度.气球不会被实验所“磨损”. 分析: 1.dp[i][j]表示第i个气球,测试j次所能确定的最高楼层. 2.假设第i-1个气球测试j-1 ...
- VC++ DLL 3 动态链接库
前面先介绍了静态链接库的方式提供了函数结构的方法,现在就来说下,如果用非MFC的动态链接库要怎么实现,这个过程稍微复杂一点点,但是基本也都是一个套路下来. 1.新建一个工程: 2.编写cpp文件和头文 ...
- MySQL硬核干货:从磁盘读取数据页到缓冲池时,免费链表有什么用?
1.数据库启动的时候,是如何初始化Buffer Pool的? 现在我们已经搞明白一件事儿了,那就是数据库的Buffer Pool到底长成个什么样,大家想必都是理解了 其实说白了,里面就是会包含很多个缓 ...
- .NET httpClient Post请求,GET请求方法
1.后端是WebAPI,POST请求,修饰符是[FromBody]的字符串,[FromBody]修饰的时候数据是来自body部分,而不是来自url部分,所以后端取值会自动映射出数据,比如后端是这样的, ...
- mybatis-地区三表生成地区树
package com.dhht.manager.vo.area; import lombok.Data; import java.io.Serializable;import java.util.L ...
- 经理人和app开发者大打出手,说明这个市场已经畸形变态?
日前,一件民生事件在网络上广为流传,成为人们热议的话题:中国平安的产品经理向app开发者提了一个需求,要求用户app的主题颜色能根据手机外壳自动调整,可能是开发人员觉得这个要求太不合理,而且感到十分绝 ...
- Java 面向对象的帮助文档制作(6)
set classPath=c:\myclass; 设置path路径