当前位置:网站首页>Leetcode(452)——用最少数量的箭引爆气球
Leetcode(452)——用最少数量的箭引爆气球
2022-06-26 20:43:00 【SmileGuy17】
Leetcode(452)——用最少数量的箭引爆气球
题目
有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [xstart, xend] 表示水平直径在 xstart 和 xend之间的气球。你不知道气球的确切 y 坐标。
一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被 引爆 。可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。
给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数 。
示例 1:
输入:points = [[10,16],[2,8],[1,6],[7,12]]
输出:2
解释:气球可以用2支箭来爆破:
- 在x = 6处射出箭,击破气球[2,8]和[1,6]。
- 在x = 11处发射箭,击破气球[10,16]和[7,12]。
示例 2:
输入:points = [[1,2],[3,4],[5,6],[7,8]]
输出:4
解释:每个气球需要射出一支箭,总共需要4支箭。
示例 3:
输入:points = [[1,2],[2,3],[3,4],[4,5]]
输出:2
解释:气球可以用2支箭来爆破:
- 在x = 2处发射箭,击破气球[1,2]和[2,3]。
- 在x = 4处射出箭,击破气球[3,4]和[4,5]。
提示:
- 1 1 1 <= points.length <= 1 0 5 10^5 105
- points[i].length == 2 2 2
- − 2 31 -2^{31} −231 <= xstart < xend <= 2 31 − 1 2^{31 - 1} 231−1
题解
方法一:贪心算法
思路
先按左端点对气球进行升序排序。然后进行重叠判断,用 right 表示上一个区间的右端点(即可以一根弓箭射穿的重叠部分的右端点)。如果重叠则用重叠部分的右端点来指代多个区间——即如果存在可以一根弓箭射爆多个气球的位置则用该位置表示这些气球。

代码实现
我自己写的:
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
int ans = 0; // 弓箭总数
// 将数组按左端点进行升序排序
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b){
return a[0] < b[0];});
int right; // 上一个区间的右端点
for(auto &it: points){
// 如果重叠则用重叠部分来指代多个区间
if(ans != 0 && right >= it[0]){
right = it[1] < right? it[1]: right;
}else{
ans++;
right = it[1];
}
}
return ans;
}
};
复杂度分析
时间复杂度: O ( n log n ) O(n \log n) O(nlogn),其中 n n n 是数组 points \textit{points} points 的长度。sort 排序函数的时间复杂度为 O ( n log n ) O(n \log n) O(nlogn),对所有气球进行遍历并计算答案的时间复杂度为 O ( n ) O(n) O(n),其在渐进意义下小于前者,因此可以忽略。
空间复杂度: O ( log n ) O(\log n) O(logn),即为排序需要使用的栈空间。
边栏推荐
猜你喜欢

leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)

How to install mysql8.0 database under Windows system? (Graphic tutorial)

leetcode刷题:字符串06(实现 strStr())

The relationship between the development of cloud computing technology and chip processor

阿里云个人镜像仓库日常基本使用

leetcode刷题:字符串02( 反转字符串II)

QT based "synthetic watermelon" game
Detailed explanation of stored procedures in MySQL

Daily basic use of alicloud personal image warehouse

宝藏又小众的覆盖物PBR多通道贴图素材网站分享
随机推荐
Dynamic parameter association using postman
[serial] shuotou O & M monitoring system 01 overview of monitoring system
C language simple login
c语言99乘法表
孙老师版本JDBC(2022年6月12日21:34:25)
How to install mysql8.0 database under Windows system? (Graphic tutorial)
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
清华大学就光刻机发声,ASML立马加紧向中国出口光刻机
Separate save file for debug symbols after strip
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
JWT operation tool class sharing
宝藏又小众的覆盖物PBR多通道贴图素材网站分享
云计算技术的发展与芯片处理器的关系
The two files are merged into a third file.
【protobuf 】protobuf 昇級後帶來的一些坑
[Bayesian classification 4] Bayesian network
leetcode刷题:字符串02( 反转字符串II)
leetcode刷题:哈希表08 (四数之和)
Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
Feitian +cipu body brings more imagination to the metauniverse