当前位置:网站首页>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 .
边栏推荐
- 八大排序-----------快速排序
- 【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?
- NoClassDefFoundError 处理
- Joiner.on和stream().map联合使用技巧
- 【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
- MySql-面试题
- SQLyog 安装和配置教程
- JUC concurrent knowledge points
- Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem
- 关于时间复杂度的个人看法
猜你喜欢

顺序表和链表

【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识

Leetcode 26. delete duplicates in the ordered array

【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略

官方教程 Redshift 07 Instances and Proxy

Leetcode 1. sum of two numbers

Computer network interview questions

markdown与Typora

ML10 self study notes SVM

From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
随机推荐
leetcode---技巧
关于时间复杂度的个人看法
【软件工程之美 - 专栏笔记】27 | 软件工程师的核心竞争力是什么?(上)
LeetCode #35.搜索插入位置
LeetCode #26.删除有序数组中的重复项
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
2022暑初二信息竞赛学习成果分享1
多线程和并发
【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
链表--------------------尾插法
NOI Online 2022普及组 题解&个人领悟
JUC concurrent knowledge points
Eight sorts ----------- bubble sort
Zero basics FPGA (5): counter of sequential logic circuit design (with introduction to breathing lamp experiment and simple combinational logic design)
Based on stc51: schematic diagram and source code of four axis flight control open source project (entry-level DIY)
LeetCode #9.回文数
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
三国演义章节内容
八大排序-----------------堆排序
无符号右移