当前位置:网站首页>2022.04.21(LC_435_无重叠区间)
2022.04.21(LC_435_无重叠区间)
2022-04-21 18:57:00 【Leeli9316】

方法:贪心
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
//按右端点升序排列
Arrays.sort(intervals, (o1, o2) -> o1[1] - o2[1]);
int ans = 0;
int pos = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
//找到互不重叠的区间,并不断更新最小的右端点
if (intervals[i][0] >= pos) {
pos = intervals[i][1];
//需要移除的重叠区间
} else {
ans++;
}
}
return ans;
}
}
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length == 0) return 0;
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] interval1, int[] interval2) {
return interval1[1] - interval2[1];
}
});
int pos= intervals[0][1];
int ans = 1;
for (int i = 1; i < intervals.length; i++) {
//需要保留的区间
if (intervals[i][0] >= pos) {
ans++;
pos= intervals[i][1];
}
}
//区间总数减去需要保留的区间,即为需要移除的区间
return intervals.length - ans;
}
}
版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Leeli9316/article/details/124322569
边栏推荐
- The video course "vite Learning Guide - webify deployment project based on Tencent cloud" was launched "cloud + community"
- 美国IBM研究院Payel Das等人NMI论文:优化分子的通用型机器学习框架
- 一针见血式处理doGet&doPost中文乱码问题
- 【持续更新中】C#常见问题及其解决(VS2019)
- The interviewer asked me about my understanding of the transaction isolation mechanism? That's my answer
- SVG系列——2,绘制一些形状
- VB judges whether a number is a prime number.
- 无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐
- Which wireless Bluetooth headset is easier to use? 2022 Bluetooth headset recommendations
- 【js学习笔记四十】复杂工厂模式
猜你喜欢

Developer tools top 100 list

OBS access network camera

86 R k-means,层次,EM聚类介绍

Channel Allocation 别用四色定理

Typescript quick start, class, public, private, extensions

flutter xcode打包发布失败 Error.90165

What brand of wireless headphones has the best quality? Recommended Bluetooth headset with good reputation

无线蓝牙耳机哪款比较好用?2022蓝牙耳机推荐

Svg series - 2, draw some shapes

一文读懂Seek Tiger推出创世节点的意义
随机推荐
flutter xcode打包发布失败 Error.90165
CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识
LeetCode1765. 地图中的最高点(BFS)
【ES6】let、const、解构赋值、模板字符串
解析机器人智能推理规划
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Clickhouse installation configuration
图的深度优先搜索DFS(邻接矩阵和邻接表版)
有PMP证书一个月能拿多少钱
【深度之眼】情感分析——循环神经网络用于多任务学习的文本分类TextRNN
Kotlin | these things you should know about lazy
ubutnu安装go
Tencent cloud database tdsql -- blog database migration practice
108. 将有序数组转换为二叉搜索树(图文并解)
Database advanced learning: storage engine
天旦发布云原生智能运维产品线,推出全球首个全栈可观测性技术标准
Qianxin monitoring equipment --- Jiaotu
URL转码问题:URLDecoder.decode(str)过时,解决:decode(String s, String enc) throws UnsupportedEncodingExceptio
Semaphore and exchange of concurrent tools
真无线耳机什么牌子好?高颜值旗舰蓝牙耳机