[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 ...
随机推荐
- 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决
下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...
- django-使用类视图
视图函数views.py中 from django.shortcuts import render, redirect from django.http import HttpResponse, Js ...
- 5 Successful Business Models for Web-Based Open-Source Projects
https://handsontable.com/blog/articles/2016/3/5-successful-business-models-for-web-based-open-source ...
- page内置对象
- [Codeforces 1251F]Red-White Fence
Description 题库链接 给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\).让你用这些木板组成一段围栏,要满足: 只用一块红木板,且所有白木板的长度均严格小于 ...
- Joint Approximative Diagonalization of Eigen matrix (JADE)
特征矩阵联合相似对角化算法[1]. Cardoso于1993年提出的盲信号分离具有代表性的一种算法.是一种基于四阶累积量特征矩阵近似联合对角化盲分离算法.该算法将目标函数最大化问题等价于一组四阶累积量 ...
- ROM
ROM 是 read only memory的简称,表示只读存储器,是一种半导体存储器.只读存储器(ROM)是一种在正常工作时其存储的数据固定不变,其中的数据只能读出,不能写入,即使断电也能够保留数据 ...
- [Gradle] 解决高德 jar 包打包到 aar 后 jar 包中的 assets 内容丢失
问题描述 将高德 SDK 的 jar 包放到 android library project libs 目录下,发布为 aar 包后,发现高德 jar 包中的 assets 目录下的内容不见了 原因见 ...
- 73: luogu 2014 树形dp
$des$ 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有 ...
- 【loj2262】【CTSC2017】网络
题目 一颗\(n\)个点的树,求加入一条边点之后两点间最长距离的最小值 : \(n \le 100000\) ; 题解 首先加入边的两个端点一定在直径上面,先\(dfs\)拎出直径来讨论(下标只代表直 ...