当前位置:网站首页>2022.04.21(LC_452_用最少数量的箭引爆气球)
2022.04.21(LC_452_用最少数量的箭引爆气球)
2022-04-21 18:57:00 【Leeli9316】

方法:贪心
class Solution {
public int findMinArrowShots(int[][] points) {
//对[[-2147483646,-2147483645],[2147483646,2147483647]]来说
//用Arrays.sort(points, (o1, o2) -> o1[1] - o2[1]);进行排序,相减会溢出
//应该按以下写法对Xend升序排列
Arrays.sort(points, (o1, o2) -> Integer.compare(o1[1], o2[1]));
int ans = 0;
int i = 0;
while (i < points.length) {
//当前气球的Xend坐标
int end = points[i][1];
int j = i + 1;
//如果下一个气球的Xstart坐标小于等于上一个气球的Xend
//说明它们可以被同一支箭引爆
for ( ; j < points.length; j++) {
int next = points[j][0];
if (next > end) break;
}
ans++;
i = j;
}
return ans;
}
}
class Solution {
public int findMinArrowShots(int[][] points) {
//对[[-2147483646,-2147483645],[2147483646,2147483647]]来说
//用Arrays.sort(points, (o1, o2) -> o1[1] - o2[1]);进行排序,相减会溢出
//应该按以下写法对Xend升序排列
Arrays.sort(points, (o1, o2) -> Integer.compare(o1[1], o2[1]));
int ans = 1;
//排序后右边界位置最靠左的气球
int pos = points[0][1];
for (int[] balloon : points) {
//找到第一个不能被上一支箭引爆的气球
if (balloon[0] > pos) {
ans++;
//更新右边界位置最靠左的气球的位置
pos = balloon[1];
}
}
return ans;
}
}
版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Leeli9316/article/details/124316247
边栏推荐
- Is it useful for newly graduated college students to take the PMP test?
- Typescript quick start, class, public, private, extensions
- What does the fragment state in viewpager save
- Crystal Chem活性 GIP ELISA 试剂盒说明书
- SQL data type
- [Wangdao postgraduate entrance examination 3] OSI seven layer reference model, TCP / IP reference model and five layer reference model
- 669. 修剪二叉搜索树
- Leetcode 824.山羊拉丁文
- 有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机
- 无线蓝牙耳机哪款比较好用?2022蓝牙耳机推荐
猜你喜欢
![[JS learning notes 40] complex factory mode](/img/a3/68e4444156226b1d7361fa6ba0041b.png)
[JS learning notes 40] complex factory mode

TypeScript快速上手,class,public,private,extends

How does PR open MKV files? How can MKV files be converted to MP4 and how can MP4 be consumed by ImageJ?

Dx12 rendering engine directory

数据库进阶学习:索引分类和创建语法

【大话云原生】负载均衡篇-小饭馆客流量变大了

Kotlin | these things you should know about lazy

使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD

【持续更新中】C#常见问题及其解决(VS2019)

Chinese NER Using Lattice LSTM 论文解读
随机推荐
论持续发展创客教育的意义
【js学习笔记四十一】单体模式
T-SQL language of SQL Server database
Abbexa 山羊 F(ab‘)2 IgG 同种型对照
Dx12 rendering engine directory
我好菜,发慌
SQL data type
[04][02][02] SPI 机制
解析机器人智能推理规划
Database advanced learning: index classification and creation syntax
MySQL的吞吐量
排序会了递归,不学非递归太可惜了
Abbexa MPO (FITC) / CD3 (PE) 组合抗体
Chinese NER Using Lattice LSTM 论文解读
TypeScript快速上手,class,public,private,extends
Major event review Carnival link construction
如何成为一个好的项目经理?
编程中的Context(上下文)
有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机
【深度之眼】情感分析——循环神经网络用于多任务学习的文本分类TextRNN