[LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note:
- You may assume the interval's end point is always bigger than its start point.
- Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
Example 1:
Input: [ [1,2], [2,3], [3,4], [1,3] ] Output: 1 Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Example 2:
Input: [ [1,2], [1,2], [1,2] ] Output: 2 Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
Example 3:
Input: [ [1,2], [2,3] ] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
这道题给了我们一堆区间,让求需要至少移除多少个区间才能使剩下的区间没有重叠,那么首先要给区间排序,根据每个区间的 start 来做升序排序,然后开始要查找重叠区间,判断方法是看如果前一个区间的 end 大于后一个区间的 start,那么一定是重复区间,此时结果 res 自增1,我们需要删除一个,那么此时究竟该删哪一个呢,为了保证总体去掉的区间数最小,我们去掉那个 end 值较大的区间,而在代码中,我们并没有真正的删掉某一个区间,而是用一个变量 last 指向上一个需要比较的区间,我们将 last 指向 end 值较小的那个区间;如果两个区间没有重叠,那么此时 last 指向当前区间,继续进行下一次遍历,参见代码如下:
解法一:
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
int res = , n = intervals.size(), last = ;
sort(intervals.begin(), intervals.end());
for (int i = ; i < n; ++i) {
if (intervals[i][] < intervals[last][]) {
++res;
if (intervals[i][] < intervals[last][]) last = i;
} else {
last = i;
}
}
return res;
}
};
我们也可以对上面代码进行简化,主要利用三元操作符来代替 if 从句,参见代码如下:
解法二:
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.empty()) return ;
sort(intervals.begin(), intervals.end());
int res = , n = intervals.size(), endLast = intervals[][];
for (int i = ; i < n; ++i) {
int t = endLast > intervals[i][] ? : ;
endLast = t == ? min(endLast, intervals[i][]) : intervals[i][];
res += t;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/435
类似题目:
Data Stream as Disjoint Intervals
Minimum Number of Arrows to Burst Balloons
参考资料:
https://leetcode.com/problems/non-overlapping-intervals/
https://leetcode.com/problems/non-overlapping-intervals/discuss/91713/Java%3A-Least-is-Most
https://leetcode.com/problems/non-overlapping-intervals/discuss/91700/Concise-C%2B%2B-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Non-overlapping Intervals 非重叠区间的更多相关文章
- [LeetCode] 435. Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 435 Non-overlapping Intervals 无重叠区间
给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- C#LeetCode刷题之#56-合并区间(Merge Intervals)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3676 访问. 给出一个区间的集合,请合并所有重叠的区间. 输入: ...
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
Intervals Time Limit: 1 Second Memory Limit:65536 KB Special Judge Chiaki has n intervals ...
- [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...
- Leetcode 435.无重叠区间
无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触" ...
随机推荐
- ORA-01034:ORACLE not available问题的解决方法
同时在自己电脑上装了oracle客户端和服务器,上次还能用呢,这次突然用不了. [oracle@R39i oracle]$ sqlplus scott/tiger SQL*Plus: Release ...
- SVN服务器和客户端安装教程
SVN是什么?有何用? SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁 ...
- Apworks框架实战(五):EasyMemo的领域模型设计
在上一讲中,我们已经新建了一个聚合根对象Account,并已经可以开始设计领域模型了.在这一讲中,我们会着重介绍EasyMemo领域模型的分析和设计,并引入Visual Studio Ultimate ...
- 如果你也会C#,那不妨了解下F#(3):F#集合类型和其他核心类型
本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-3.html 在第一篇中,我们介绍了一些基础数据类型,其实那篇标题中不应该含有"F#&q ...
- 【转载】C#怎么判断字符是不是汉字
支持并尊重原创!原文地址:http://jingyan.baidu.com/article/2c8c281deb79ed0008252af1.html 判断一个字符是不是汉字通常有三种方法,第1种用 ...
- 黑马程序员——ARC机制总结和用ARC建立模型
ARC 全称:Automatic Reference Counting 使用ARC 只需要在建立一个新的项目的时候把 下面的√打上 Xcode5以后都会默认建议开发者使用ARC机制 新的项目中如果有部 ...
- php分页原理
<?php //包含连接MySQL的文件 include "conn.php"; //分页的相关变量 $pagesize = 5; //每页显示条数 //获取地址栏中传递的p ...
- 如何解决例如i++的线程不安全性
AtomicBoolean.AtomicInteger.AtomicLong.AtomicReference 各种原子性关键字,可以解决比如i++的线程不安全性的因素
- windows7 x64下maven安装和配置
http://maven.apache.org/download.cgi下载maven 环境配置 验证配置是否成功 本地仓库配置 这是原来的配置文件: 更改为: link 离线安装 eclipse m ...
- git 管理
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #3e3e3e; bac ...