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:

  1. You may assume the interval's end point is always bigger than its start point.
  2. 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

类似题目:

Find Right Interval

Data Stream as Disjoint Intervals

Insert Interval

Merge Intervals

Maximum Length of Pair Chain

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] 435. Non-overlapping Intervals 非重叠区间的更多相关文章

  1. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  2. LeetCode OJ:Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given  [1,3],[2,6],[8, ...

  3. 435 Non-overlapping Intervals 无重叠区间

    给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意:    可以认为区间的终点总是大于它的起点.    区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...

  4. Java实现 LeetCode 435 无重叠区间

    435. 无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触& ...

  5. Leetcode 435.无重叠区间

    无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触" ...

  6. LeetCode 56. Merge Intervals (合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~

    Intervals Time Limit: 1 Second      Memory Limit:65536 KB      Special Judge Chiaki has n intervals ...

  8. [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点

    Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...

  9. 【LeetCode】435-无重叠区间

    题目描述 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触", ...

随机推荐

  1. PHP自动加载-spl_autoload_register

    spl_autoload_register 自动加载spl : Standard PHP library (标准PHP库) 首先来了解 __autoload print.class.php <? ...

  2. 解锁云原生 AI 技能|在 Kubernetes 上构建机器学习系统

    本系列将利用阿里云容器服务,帮助您上手 Kubeflow Pipelines. 介绍 机器学习的工程复杂度,除了来自于常见的软件开发问题外,还和机器学习数据驱动的特点相关.而这就带来了其工作流程链路更 ...

  3. Logstash:处理多个input

    Logstash:处理多个input Logstash的整个pipleline分为三个部分: input插件:提取数据. 这可以来自日志文件,TCP或UDP侦听器,若干协议特定插件(如syslog或I ...

  4. RookeyFrame在线新增模块

    今天给大家演示下在线新增模块的功能,在线新增模块跟在vs中写model实体类区别不大,线上新增少了手动初始化的过程,新增后模块同样具备新增.修改.删除.查看.导入.导出.复制.批量编辑.回收站.草稿箱 ...

  5. CSS覆盖问题的说明

    最近在写css的时候,由于经常使用到很长的多级选择器,而碰到一些样式被覆盖或者覆盖不了的情况是相当的郁闷,所以专门花了一些时间对一些选择器做了对比测试.这里先说明一下,由于ie6不支持css2.0选择 ...

  6. UWP使用Microsoft.Data.Sqlite的记录

    我在UWP中使用SQLite数据库时,并没有使用网上的SQLite for Universal App Platform方案,而使用了Microsoft和SQLite社区一起维护的Microsoft. ...

  7. Linux链接文件——软连接和硬链接

    Linux链接文件——软连接和硬链接 摘要:本文主要介绍了Linux系统中的链接文件. 文件系统 在Linux系统中,将文件分为两个部分:用户数据和元数据. 元数据(inode) 元数据即文件的索引节 ...

  8. 怎么把使用vuepress搭建的博客部署到Github Pages

    推荐在这里阅读效果更佳 背景 网上搜了很多教程,包括官网的教程,但是还是费了一番功夫, 如果你使用自动化部署脚本部署不成功的话,可以参考我的这个笨方法 这是部署后的效果 前提 我假设你本地运行OK, ...

  9. wpf 打开win8系统软件盘

    三个函数 一) /// <summary> /// 判断进程是否正在运行 /// </summary> /// <param name="process&quo ...

  10. iOS开发之--为UITextField监听数值变化的三种方法

    项目中有个验证码输入直接验证跳转页面,用的RAC来监听textfield的输入值,如下: @weakify(self); [self.codeView.textField.rac_textSignal ...