当前位置:网站首页>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 question brushing: String 02 (reverse string II)
- 第2章 构建自定义语料库
- Feitian +cipu body brings more imagination to the metauniverse
- MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
- Sword finger offer II 091 Paint the house
- Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
- 论数据库的传统与未来之争之溯源溯本----AWS系列专栏
- lotus configurations
- 0 basic C language (3)
- 云计算技术的发展与芯片处理器的关系
猜你喜欢

Development of NFT for digital collection platform
MySQL中存储过程的详细详解

【山东大学】考研初试复试资料分享

Leetcode question brushing: String 06 (implement strstr())

Detailed explanation of shutter textfield
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

GEE:计算image区域内像素最大最小值

Two methods of QT to realize timer

【连载】说透运维监控系统01-监控系统概述

On the origin of the dispute between the tradition and the future of database -- AWS series column
随机推荐
leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
Background search, how to find the website background
The two files are merged into a third file.
Detailed explanation of shutter textfield
12个MySQL慢查询的原因分析
Leetcode question brushing: String 01 (inverted string)
证券开户安全吗,有没有什么危险呢
定长内存池
Many gravel 3D material mapping materials can be obtained with one click
【连载】说透运维监控系统01-监控系统概述
MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
MySQL stored procedure
Feitian +cipu body brings more imagination to the metauniverse
基于QT开发的线性代数初学者的矩阵计算器设计
【山东大学】考研初试复试资料分享
Detailed explanation of retrospective thinking
0 basic C language (1)
Three basic backup methods of mongodb
【 protobuf 】 quelques puits causés par la mise à niveau de protobuf
The source code that everyone can understand (I) the overall architecture of ahooks