当前位置:网站首页>Leetcode (452) - detonate the balloon with the minimum number of arrows
Leetcode (452) - detonate the balloon with the minimum number of arrows
2022-06-26 21:55:00 【SmileGuy17】
Leetcode(452)—— Detonate the balloon with a minimum number of arrows
subject
There are some spherical balloons attached to a wall for XY On the wall represented by the plane . The balloons on the wall are recorded in an integer array points , among points[i] = [xstart, xend] Indicates that the horizontal diameter is xstart and xend Between the balloons . You don't know the exact of the balloon y coordinate .
A bow and arrow can follow x Axis from different points Completely vertical Shoot out . In coordinates x Shoot an arrow at , If there is a balloon with a diameter starting and ending coordinates of xstart,xend, And meet xstart ≤ x ≤ xend, Then the balloon will be tipping . The number of bows and arrows that can be shot There is no limit to . Once the bow is shot , Can move forward indefinitely .
Give you an array points , Return to what must be fired to detonate all balloons Minimum Number of bows and arrows .
Example 1:
Input :points = [[10,16],[2,8],[1,6],[7,12]]
Output :2
explain : Balloons can be used 2 An arrow to blow up :
- stay x = 6 Shoot an arrow at , Break the balloon [2,8] and [1,6].
- stay x = 11 Shoot an arrow , Break the balloon [10,16] and [7,12].
Example 2:
Input :points = [[1,2],[3,4],[5,6],[7,8]]
Output :4
explain : Each balloon needs to shoot an arrow , All in all 4 An arrow .
Example 3:
Input :points = [[1,2],[2,3],[3,4],[4,5]]
Output :2
explain : Balloons can be used 2 An arrow to blow up :
- stay x = 2 Shoot an arrow , Break the balloon [1,2] and [2,3].
- stay x = 4 Shoot an arrow at , Break the balloon [3,4] and [4,5].
Tips :
- 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
Answer key
Method 1 : Greedy Algorithm
Ideas
Sort the balloons in ascending order by the left endpoint . Then make overlapping judgment , use right Represents the right endpoint of the previous interval ( That is, the right end point of the overlapping part that can be pierced by a bow ). If it overlaps, the right end of the overlapped part is used to refer to multiple intervals —— That is, if there is a position where a bow and arrow can explode multiple balloons, the position is used to represent these balloons .

Code implementation
I wrote it myself :
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
int ans = 0; // Total number of bows and arrows
// Sort the array in ascending order by the left endpoint
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b){
return a[0] < b[0];});
int right; // The right end of the previous interval
for(auto &it: points){
// If there is overlap, the overlapping part is used to refer to multiple intervals
if(ans != 0 && right >= it[0]){
right = it[1] < right? it[1]: right;
}else{
ans++;
right = it[1];
}
}
return ans;
}
};
Complexity analysis
Time complexity : O ( n log n ) O(n \log n) O(nlogn), among n n n It's an array points \textit{points} points The length of .sort The time complexity of the sorting function is O ( n log n ) O(n \log n) O(nlogn), The time complexity of traversing all balloons and calculating the answer is O ( n ) O(n) O(n), It is smaller than the former in the progressive sense , So we can ignore .
Spatial complexity : O ( log n ) O(\log n) O(logn), This is the stack space needed for sorting .
边栏推荐
- 【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】
- Data governance does everything
- Can compass open an account for stock trading? Is it safe?
- leetcode:710. 黑名单中的随机数【映射思维】
- DLA模型(分类模型+改进版分割模型) + 可变形卷积
- vulnhub之DC9
- 如何在 SAP BTP 平台上启用 HANA Cloud 服务
- 在Flutter中解析复杂的JSON
- Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
- 简析攻防演练中蓝队的自查内容
猜你喜欢

协同过滤进化版本NeuralCF及tensorflow2实现

Matrix derivation and its chain rule

How to analyze financial expenses

Configuring assimp Library in QT environment (MinGW compiler)

DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability

Implementation of collaborative filtering evolution version neuralcf and tensorflow2

What are the accounting elements

VB.net类库——4给屏幕截图,裁剪

CVPR 2022 | 美团技术团队精选论文解读

矩阵求导及其链式法则
随机推荐
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
Leetcode(763)——划分字母区间
MacOS环境下使用HomeBrew安装[email protected]
AI智能抠图工具--头发丝都可见
Some ways out for older programmers
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
聊聊我的远程工作体验 | 社区征文
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
[leetcode]- linked list-2
Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,
这个算BUG吗?乱填的字母是否可以关闭
Release of dolphin scheduler video tutorial in Shangsi Valley
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
协同过滤进化版本NeuralCF及tensorflow2实现
PostgreSQL notes
JupyterLab 常用配置
YOLOv6:又快又准的目标检测框架开源啦
Talk about my remote work experience | community essay solicitation
The latest 2022 research review of "continuous learning, CL"