当前位置:网站首页>leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
2022-07-28 08:15:00 【okokabcd】
一、题目大意
标签: 贪心
https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons
有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [xstart, xend] 表示水平直径在 xstart 和 xend之间的气球。你不知道气球的确切 y 坐标。
一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被 引爆 。可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。
给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数 。
示例 1:
输入:points = [[10,16],[2,8],[1,6],[7,12]]
输出:2
解释:气球可以用2支箭来爆破:
-在x = 6处射出箭,击破气球[2,8]和[1,6]。
-在x = 11处发射箭,击破气球[10,16]和[7,12]。
示例 2:
输入:points = [[1,2],[3,4],[5,6],[7,8]]
输出:4
解释:每个气球需要射出一支箭,总共需要4支箭。
示例 3:
输入:points = [[1,2],[2,3],[3,4],[4,5]]
输出:2
解释:气球可以用2支箭来爆破:
- 在x = 2处发射箭,击破气球[1,2]和[2,3]。
- 在x = 4处射出箭,击破气球[3,4]和[4,5]。
提示:
- 1 <= points.length <= 105
- points[i].length == 2
- -231 <= xstart < xend <= 231 - 1
二、解题思路
区间重叠问题,一般都要想到贪心(局部最优等于全局最优)。本题给我们一堆大小不等的气球,用区间范围来表示气球大小,可能会有重叠区间,我们用最少的箭打爆所有气球。我们把所有区间按照右边界进行排序,因为每个气球都要被打破,因此排序得到的第一组数据我们一定要使用。只要沿着该数据最右边界的位置进行射箭一定能打破尽可能多的气球。然后依次移动射箭的位置,进行统计即可。
三、解题方法
3.1 Java实现
public class Solution {
public int findMinArrowShots(int[][] points) {
// 二维数组的排序??
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;
}
}
四、总结小记
- 2022/7/27 恢复每日一题
边栏推荐
- 478-82(56、128、718、129)
- Vrrp+mstp configuration details [Huawei ENSP experiment]
- Network interface network crystal head RJ45, Poe interface definition line sequence
- Centralized log management with sentry
- Kubernetes cluster configuration DNS Service
- Go channel
- Will sqlserver CDC 2.2 generate table locks when extracting large tables from the source
- SQL injection - pre Foundation
- 12 common design ideas of design for failure
- Hundreds of billions of it operation and maintenance market has come to the era of speaking by "effect"
猜你喜欢

Flink window & time principle

DAPP safety summary and typical safety incident analysis

Smart software completed round C financing, making Bi truly "inclusive"

C language array pointer and pointer array discrimination, analysis of memory leakage
![Chapter 2-2 calculation of piecewise function [1]](/img/40/cad6bf92849624199af0fd1ba1d433.jpg)
Chapter 2-2 calculation of piecewise function [1]

Machine learning: self paced and fine tuning

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

How to obtain the subordinate / annotation information of KEGG channel

Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?

Explain cache consistency and memory barrier
随机推荐
49 opencv deep analysis profile
10、学习MySQL LIKE 子句
Customer first | domestic Bi leader, smart software completes round C financing
Div tags and span Tags
Oracle SQL problems
Centralized log management with sentry
1w5 words to introduce those technical solutions of distributed system in detail
Baidu AI Cloud Jiuzhou district and county brain, depicting a new blueprint for urban and rural areas!
Hou Jie STL standard library and generic programming
MDM数据质量应用说明
为什么 ThreadLocal 可以做到线程隔离?
Argocd Web UI loading is slow? A trick to teach you to solve
置顶各大平台,22版面试核心知识解析笔记,强势上榜
Do you know the five minute rule and the ten byte rule?
Quickly build a gateway service, dynamic routing and authentication process, and watch the second meeting (including the flow chart)
You're not still using xshell, are you? This open source terminal tool is yyds!
Introduction of functions in C language (blood Book 20000 words!!!)
Detailed explanation of the basic use of express, body parse and express art template modules (use, route, path matching, response method, managed static files, official website)
Kubernetes cluster configuration dashboard service
SQL injection - pre Foundation