当前位置:网站首页>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 .
边栏推荐
- 单链表面试题
- 三国演义章节内容
- LeetCode #35.搜索插入位置
- [beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
- [beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
- 官方教程 Redshift 06 Opt参数
- Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
- leetcode刷题笔记 763.划分字母区间(中等)
- Leetcode 1. sum of two numbers
- Multithreading and concurrency
猜你喜欢

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

LeetCode #189.轮转数组

官方教程 Redshift 06 Opt参数

2022 spring move - core technology FPGA post technical aspects (one side experience)

Dynamic planning summary

2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)

leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)

markdown与Typora

官方教程 Redshift 05 system参数详细解释

Leetcode 1. sum of two numbers
随机推荐
Joiner.on和stream().map联合使用技巧
关于时间复杂度的个人看法
FPGA based: moving target detection (supplementary simulation results, available)
关于【链式前向星】的自学理解
LeetCode #7.整数反转
JVM内存结构
滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
Ml9 self study notes
Computer network interview questions
【Leetcode刷题】数组1——双指针
爬虫Requests库的一些简单用法
Understanding of synchronized eight lock phenomenon
JUC concurrent knowledge points
Sqlyog installation and configuration tutorial
Leetcode 167. sum of two numbers II - input ordered array
【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
IDEA安装scala
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
LeetCode #189.轮转数组
【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员