当前位置:网站首页>Leetcode 452. minimum number of arrows to burst balloons (medium)
Leetcode 452. minimum number of arrows to burst balloons (medium)
2022-07-28 09:15:00 【okokabcd】
One 、 The main idea of the topic
label : greedy
https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons
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 <= points.length <= 105
- points[i].length == 2
- -231 <= xstart < xend <= 231 - 1
Two 、 Their thinking
Interval overlap problem , Generally, we should think of greed ( Local optimum is equal to global optimum ). This topic gives us a bunch of balloons of different sizes , The balloon size is represented by the range , There may be overlapping intervals , We used the fewest arrows to blow up all the balloons . We sort all the intervals according to the right boundary , Because every balloon will be broken , Therefore, we must use the first set of data obtained by sorting . As long as you shoot along the position of the rightmost boundary of the data, you will be able to break as many balloons as possible . Then move the archery position in turn , Just make statistics .
3、 ... and 、 How to solve the problem
3.1 Java Realization
public class Solution {
public int findMinArrowShots(int[][] points) {
// Sorting two-dimensional arrays ??
Arrays.sort(points, Comparator.comparingInt(o -> o[1]));
int res = 1;
int end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] <= end) {
end = Math.min(end, points[i][1]);
} else {
res++;
end = points[i][1];
}
}
return res;
}
}
Four 、 Summary notes
- 2022/7/27 Resume one question per day
边栏推荐
- 【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
- [592. Fraction addition and subtraction]
- ES6 let and Const
- Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
- Principle of line of sight tracking and explanation of the paper
- Send a message to the background when closing the page
- C#简单调用FMU ,进行仿真计算
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- Eight ways to solve EMC and EMI conducted interference
- Go channel
猜你喜欢

推荐一个摆脱变量名纠结的神器和批量修改文件名方法

What are the main uses of digital factory management system

No one wants to tell the truth about kubernetes secret

Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches

Realize batch data enhancement | use of keras imagedatagenerator

Path and attribute labels of picture labels

478-82(56、128、718、129)

View the dimensions of the list

Why setting application.targetframerate doesn't work

Introduction of functions in C language (blood Book 20000 words!!!)
随机推荐
The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
You're not still using xshell, are you? This open source terminal tool is yyds!
【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
关闭页面时向后台发送消息
v-bind指令的详细介绍
Digital signatures and Ca certificates
信息学奥赛一本通 1617:转圈游戏 | 1875:【13NOIP提高组】转圈游戏 | 洛谷 P1965 [NOIP2013 提高组] 转圈游戏
Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
Post it notes -- 45 {packaging of the uniapp component picker, for data transmission and processing -- Based on the from custom packaging that will be released later}
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
mysql主从架构 ,主库挂掉重启后,从库怎么自动连接主库
How to obtain the subordinate / annotation information of KEGG channel
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture
2022高压电工考试模拟100题及模拟考试
1.5 merge\rebase\revert\stash\branch
Different HR labels
Machine learning (11) -- time series analysis
Mysql5.7.38 start keepalived in the container
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
Review the past and know the new MySQL isolation level