[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 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).
给一堆气球,用区间[start,end]来表示气球大小,会有重叠区间。箭从某一个位置发射,只要区间包含这个点的就可以被射中。求用最少的箭数将所有的气球打爆。
解法:贪婪算法Greedy,先给区间排序,遍历区间,第一个区间时,先加1箭,记录区间的end,然后比较后面的区间的start,如果start <= end,说明两个区间有重合,箭从重合区间发射就可以同时打爆这两个气球。重新记录这两个区间里end小的值,在和后面的气球比较。如果start > end,说明没有重合区间,得在发射1箭,箭数加1。遍历结束就能得到所需的最少箭数。
Java:
public class Solution {
public int findMinArrowShots(int[][] points) {
if(points==null || points.length==0) return 0; Arrays.sort(points, ( x , y) -> x[0] == y[0] ? x[1] - y[1] : x[0] - y[0]);
int count = 1;
int arrowLimit = points[0][1];
//贪心法,基于上一个箭,记录当前能够射穿的所有
for(int i = 1;i < points.length;i++) {
if(points[i][0] <= arrowLimit) {
arrowLimit = Math.min(arrowLimit, points[i][1]);
} else {
count++;
arrowLimit = points[i][1];
}
}
return count;
}
}
Python:
class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
if not points:
return 0 points.sort() result = 0
i = 0
while i < len(points):
j = i + 1
right_bound = points[i][1]
while j < len(points) and points[j][0] <= right_bound:
right_bound = min(right_bound, points[j][1])
j += 1
result += 1
i = j
return result
C++:
class Solution {
public:
int findMinArrowShots(vector<pair<int, int>>& points) {
if (points.empty()) {
return 0;
} sort(points.begin(), points.end()); int result = 0;
for (int i = 0; i < points.size(); ++i) {
int j = i + 1;
int right_bound = points[i].second;
while (j < points.size() && points[j].first <= right_bound) {
right_bound = min(right_bound, points[j].second);
++j;
}
++result;
i = j - 1;
}
return result;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球的更多相关文章
- [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 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 贪心: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] 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 ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- [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 ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
随机推荐
- 51nod 2502 最多分成多少块
小b有个长度为n的数组a,她想将这个数组排序. 然而小b很懒,她觉得对整个数组排序太累了,因此她请你将a分成一些块,使得她只需要对每一块分别排序,就能将整个数组排序. 请问你最多能把a分成多少块. 保 ...
- 编程小白入门分享二:IntelliJ IDEA的入门操作小知识
idea简介 IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境.IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支 ...
- SSH——ssh_exchange_identification: read: Connection reset by peer
前言 ssh远程连接出错 步骤 查看ssh的详细信息 [root@pre-nginx02 ~]# ssh -v 192.168.1.164 OpenSSH_6.6.1, OpenSSL 1.0.1e- ...
- flume的sink写入hive表
flume的配置文件如下: a1.sources=r1 a1.channels=c1 a1.sinks=s1 a1.sources.r1.type=netcat a1.sources.r1.bind= ...
- Sql语句中Like嵌套用法
一般的Like用法: SELECT U_NAME FROM T_USER WHERE U_NAME LIKE '%A%' 但是,我此次like关键字后面的对应值是一个变量,需要用select语句来实现 ...
- angular和datatables一起使用的时候,出现TypeError: Cannot Read Property "childNodes" Of Undefined In AngularJS
在anguglar中注入'$timeout' 然后: $timeout(function{},0,false);
- ICMP隧道
参考文章:http://www.sohu.com/a/297393423_783648
- mysql 查询账户
查询 mysql 的存在的账户 >select user,host,password from mysql.user; # 可以查询涉及到user. host 链接权限.密码加密文件.
- Day17:web前端开发面试题
1.JavaScript 数据类型有哪些? JavaScript 变量能够保存多种数据类型:数值.字符串值.数组.对象等等: var length = 7; // 数字 var lastName = ...
- GoCN每日新闻(2019-10-11)
GoCN每日新闻(2019-10-11) GoCN每日新闻(2019-10-11) 1. golang 将数据库转换为gorm结构 https://studygolang.com/articles/2 ...