当前位置:网站首页>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 恢复每日一题
边栏推荐
- Mysql5.7.38 start keepalived in the container
- Sword finger offer
- 中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
- 看完这12个面试问题,新媒体运营岗位就是你的了
- Vs2015 use dumpbin to view the exported function symbols of the library
- IDC脚本文件运行
- Code management platform SVN deployment practice
- 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)
- Distributed system architecture theory and components
- No one wants to tell the truth about kubernetes secret
猜你喜欢

CSV file storage

There is a bug in installing CONDA environment

XMIND Zen installation tutorial

VS2015使用dumpbin 查看库的导出函数符号

You're not still using xshell, are you? This open source terminal tool is yyds!

分布式系统架构理论与组件

一年涨薪三次背后的秘密

NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ

台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】

Design for failure常见的12种设计思想
随机推荐
剑指offer
Let me teach you how to assemble a registration center?
Eight ways to solve EMC and EMI conducted interference
Machine learning: self paced and fine tuning
Flink Window&Time 原理
Oracle SQL problems
Argocd Web UI loading is slow? A trick to teach you to solve
478-82(56、128、718、129)
Kubernetes data persistence scheme
mysql5.7.38容器里启动keepalived
IDC脚本文件运行
XMIND Zen installation tutorial
Dapp安全总结与典型安全事件分析
SQL injection - pre Foundation
(IROS 2022) 基于事件相机的单目视觉惯性里程计 / Event-based Monocular Visual Inertial Odometry
You're not still using xshell, are you? This open source terminal tool is yyds!
Vrrp+mstp configuration details [Huawei ENSP experiment]
49 opencv deep analysis profile
Centralized log management with sentry
There is a bug in installing CONDA environment