当前位置:网站首页>Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)
Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)
2022-07-29 06:24:00 【Lu Yi, twelve】
1. Title Description :
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 the minimum number of bows and arrows that must be fired to detonate all balloons .
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].
2. Code :
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if(points.empty()){
return 0;
}
// Why not use two #if 0 Replace #if 1 The content in , Logically speaking, it's not faster ?
#if 1
sort(points.begin(), points.end(), [](const vector<int> &a, const vector<int> &b){
return a[1] < b[1];
});
#endif
#if 0
sort(points.begin(), points.end(), cmp);
#endif
int count = 1;
int prev = points[0][1];
for(int i = 1; i < points.size(); ++i){
if(points[i][0] > prev){
++count;
prev = points[i][1];
}
}
return count;
}
#if 0
static bool cmp(const vector<int> &a, const vector<int> &b){
return a[1] < b[1];
}
#endif
};3. Their thinking :
Since we need to use the least arrows to break through all balloons , Then each arrow should break through the balloon as much as possible ( The local optimum constitutes the global optimum ), In this way, the number of arrows used at last must be the least .
Through this picture , When the arrow moves left and right , Only this position can pass through four balloons or more , Achieve local optimal effect , This position depends on the balloon with the smallest right boundary . therefore , Just arrange the balloons in ascending order on the right boundary , After determining the position of the first arrow ( The right boundary of the first balloon , Suppose the coordinates are x1), You can make other right boundaries larger than x1, The left boundary is less than x1 All the balloons go through , Repeat the operation for the remaining balloons .
4. Code section
@. about sort Pass in the parameter , I wrote two :
The first is #if 1 The key to the content in is that the third parameter is anonymous function , The format of anonymous function is :[ External variable access method specifier ]( Parameter table ) -> Return value { Statement of body }. There are two kinds of specifiers : The first one is = It is not allowed to change the value of variables defined outside the statement ; The second kind & It is allowed to change the value of variables defined outside the statement .
The second is two #if 0 The content in , The custom function must be static Type of , because sort() Function requires parameters to be static , Personally, I think this way should run faster , I also read some other people's blogs and said the same , But in leetcode The first way is to run faster , nearly 100ms The leading , Hope to see this friend answer .
边栏推荐
- JUC concurrent knowledge points
- 循环链表和双向链表
- 抽象封装继承多态
- LeetCode #344.反转字符串
- STM32 printf问题总结 semihosting microLIB理解
- MySQL interview questions
- 【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
- 三国演义章节内容
- 官方教程 Redshift 09 Camera
- [beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
猜你喜欢

官方教程 Redshift 08 Light

Leetcode 9. palindromes

【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?

2022暑初二信息竞赛学习成果分享1

Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图

FPGA based: moving target detection (supplementary simulation results, available)

LeetCode #189.轮转数组

【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?

【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决

LeetCode #3.无重复字符的最长子串
随机推荐
数学建模心得
Add time series index to two-dimensional table
Leetcode 26. delete duplicates in the ordered array
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
Leetcode 35. search insertion location
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
LeetCode #283.移动零
Leetcode 7. integer inversion
UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
操作系统面试题
ML7 self study notes
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
markdown与Typora
LeetCode #876.链表的中间结点
Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
NOI Online 2022普及组 题解&个人领悟
抽象类以及接口
FPGA based: moving target detection (supplementary simulation results, available)
【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
官方教程 Redshift 07 Instances and Proxy