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. requests库入门14-Cookie

    因为http是没有状态的协议,上一个请求和下一个请求是没有关联.但是现实中又需要有关联,比如一个页面某个操作需要登陆之后才能进行,没有登陆就提示你登陆.为了实现这样的效果,所以出现了Cookie和Se ...

  2. centos6.8配置php-fpm(php已在apache中以模块形式运行,nginx中同时以fastcgi运行)

    location ~ \.php(.*)$ { root /mnt/www/wenyin; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; ...

  3. 029_mount bind挂载

    一. 由于公司的配置标准并不统一,交付的磁盘挂载的路径不是想要的路径,但是 1./home目录下有很重要的堡垒机登录的相关文件,还不能卸载 2.我通过pts/0登录的,这个文件描述符也是在/home目 ...

  4. python pip下载速度慢的解决方法

    pip是python内置的非常好用的下载工具,基本可以下载全部的python库.它还有一个非常好的特点,当你安装一个库的时候,它会自动帮你安装所有这个库的依赖库.完全一键式操作.非常方便.但是由于pi ...

  5. 51Nod--1384全排列

    1384 全排列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出一个字符串S(可能又重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列.例 ...

  6. word发布博客

    无向图双连通部件(双连通分量) 关节点和桥边的定义: 双连通部件的性质   每一个双连通部件应该包含至少两个顶点,除非整个无向图只包含一个顶点   如果两个双连通部件包含同一个顶点,那么这个共有的顶点 ...

  7. [转]MySQL常用Json函数和MySQL常用字符串函数

    MySQL常用Json函数:https://www.cnblogs.com/waterystone/p/5626098.html MySQL常用字符串函数:https://www.cnblogs.co ...

  8. 用KendoGrid控件 快速做CRUD

    先看效果: 主要引用的文件: <link href="/css/kendo/2014.1.318/kendo.common.min.css" rel="styles ...

  9. Android:图解四种启动模式 及 实际应用场景解说

    在一个项目中会包括着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种“后进先出”的栈结构.举个栗子,若我们多次启动同一个Activity.系统会创建多个实例依次放入 ...

  10. Android广播机制

    原文出处: Android总结篇系列:Android广播机制 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的 ...