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.

求移除多少区间后,剩余区间都是不重叠的。

先求最多能组成多少不重叠的区间,再用总区间数减去不重叠区间数。

C++:
 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool compare(const Interval& a ,const Interval& b){
return a.end < b.end ;
}
class Solution {
public:
int eraseOverlapIntervals(vector<Interval>& intervals) {
if (intervals.size() == ){
return ;
}
sort(intervals.begin() , intervals.end() , compare) ;
int cnt = ;
int end = intervals[].end ;
for(int i = ; i < intervals.size() ; i++){
if (intervals[i].start < end){
continue ;
}
end = intervals[i].end ;
cnt++ ;
}
return intervals.size() - cnt ;
}
};
 

435. Non-overlapping Intervals的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

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

  2. Leetcode Merge Intervals

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

  3. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. 【leetcode】Merge Intervals(hard)

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

  5. leetcode56. Merge Intervals

    题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...

  6. Merge Intervals

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

  7. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  8. 【Leetcode】【Hard】Merge Intervals

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

  9. Java for LeetCode 056 Merge Intervals

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

  10. [LeetCode]题解(python):056-Merge Intervals

    题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...

随机推荐

  1. 【SVN】SVN初识

    SVN 介绍 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Sub ...

  2. 巧用这19条MySQL优化【转】

    1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...

  3. 在Visual Studio中使用C++创建和使用DLL

    [什么是DLL(动态链接库)?] DLL是一个包含可由多个程序同时使用的代码和数据的库.例如:在Windows操作系统中,Comdlg32 DLL执行与对话框有关的常见函数.因此,每个程序都可以使用该 ...

  4. webstorm加载项目卡死在scanning files to index

    今天用webstorm导入项目时,需要加载node-modules文件夹,导致webstorm非常卡,页面提示scanning files to index... 网上搜到办法,记录下: 说明: 在n ...

  5. virtual box 安装centos min

    2018-4-19 22:20:40 星期四 之前不小心把用了很久的centos镜像删掉了.....这里记录下安装最小版centos的步骤 1. 安装centos 2. 开启网络, 并设置为随机启动 ...

  6. Android来电拦截及来电转移

    1. 电话拦截这个功能大家可能都知道了,就是利用反射原理调用ITelephony的隐藏方法来实现.这个就不说了,在附件的代码里有.2.拦截后提示忙音/空号/已关机/已停机这个功能其实是要用到MMI指令 ...

  7. VBS计时器2

    打开计时器,如果点击暂停,会显示你刚才事物所用的时间(以分钟为单位) dim c //控制循环 c= vbyes while c<>vbno dim a a= 60*hour(now)+m ...

  8. ie.360,qq浏览器这种ie内核浏览器默认阻止弹窗

  9. 移动端判断ios还是android终端

    <script> //判断ios还是android终端       var u = navigator.userAgent;       var isAndroid = u.indexOf ...

  10. Confluence 6 邮件队列

    需要发送的电子邮件将会在邮件队列中进行等待,Confluence 的邮件队列每分钟刷新一次.Confluence 的管理员也可以手动的刷新邮件队列中等待发送的消息. 如果在发送的时候出现了错误,那么出 ...