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. Linux就该这么学(3)-管道符、重定向与环境变量(学习笔记)

    1.Linux命令与文件读写操作有关的重定向技术: 学习目标:主要解决输出信息的保存问题. 标准覆盖输出重定向: 标准追加输出重定向 错误覆盖输出重定向 错误追加输出重定向 输入重定向 标准输入(ST ...

  2. tomcat目录映射

    环境:CentOS 6 + tomcat 7 + jdk 7 目前使用2种方法: 1.tomcat/conf/server.xml <Host name="localhost" ...

  3. Shell-cat url-list.txt | xargs wget -c

    假如你有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接: cat url-list.txt | xargs wget -c

  4. MII、RMII、GMII接口的详细介绍【转】

    转自:https://www.cnblogs.com/geekite/p/5204512.html 概述: MII (Media Independent Interface(介质无关接口)或称为媒体独 ...

  5. 关于boost中enable_shared_from_this类的原理分析

    首先要说明的一个问题是:如何安全地将this指针返回给调用者.一般来说,我们不能直接将this指针返回.想象这样的情况,该函数将this指针返回到外部某个变量保存,然后这个对象自身已经析构了,但外部变 ...

  6. $Django RESTful规范

    一 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度 ...

  7. CSS集锦

    div内容自动换行:word-wrap:break-word;word-break:break-all;

  8. Light OJ 1058

    题意: 简单的就组合数 C(m,n): 数据多,大, 要预处理: #include<bits/stdc++.h> using namespace std; typedef long lon ...

  9. JS调用摄像头并上传图片到服务器

    本功能只能把图片转成base64码上传,如何上传图片还没有修改出来,有兴趣的朋友弄出来了,请给我留下言,谢谢了! 直接上代码,需要的朋友直接复制就可以使用了. <!DOCTYPE html> ...

  10. linux学习之netstat

    netstat -anp netstat -anp -a 表示展示所有 -p 展示进程和名称 -n --numeric don't resolve names