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.

Actually, the problem is the same as "Given a collection of intervals, find the maximum number of intervals that are non-overlapping." (the classic Greedy problem: Interval Scheduling). With the solution to that problem, guess how do we get the minimum number of intervals to remove? : )

Sorting Interval.end in ascending order is O(nlogn), then traverse intervals array to get the maximum number of non-overlapping intervals is O(n). Total is O(nlogn).

开始的时候想岔了,以为是要求同一时刻overlap的最多interval数,但仔细想一想就发现不对,应该是non-overlap的interval的最大数目

1. Best solution: sorted by interval end

case 1 add current interval as another non-overlapping interval, case 2 and case 3 all get rid of the current interval

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int eraseOverlapIntervals(Interval[] intervals) {
if (intervals.length == 0) return 0;
int nonOverlap = 1;
int seq = 0;
Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
return i1.end - i2.end;
}
});
for (int i=1; i<intervals.length; i++) {
if (intervals[i].start >= intervals[seq].end) {
seq = i;
nonOverlap++;
}
}
return intervals.length - nonOverlap;
}
}

Comparator can also be rewritten as

 Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[1], i2[1]));

2. Alternatives(not the best): sort by interval start

case 1 add current interval as another non-overlapping interval, case 2 update the previous non-overlapping interval with the current one, and case 3 get rid of the current interval. So more cases need to be processed than sorted by interval end

 class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length < 1) return 0;
int seq = 0;
int nonOverlap = 1; Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0])); for (int i = 0; i < intervals.length; i ++) {
if (intervals[i][0] >= intervals[seq][1]) {
seq = i;
nonOverlap ++;
}
else if (intervals[i][1] <= intervals[seq][1]) {
seq = i;
}
} return intervals.length - nonOverlap;
}
}

Leetcode: Non-overlapping Intervals的更多相关文章

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

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

  2. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  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. Java for LeetCode 056 Merge Intervals

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

  6. [LeetCode] 56. Merge Intervals 解题思路

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

  7. leetcode[55] Merge Intervals

    题目:给定一连串的区间,要求输出不重叠的区间. Given a collection of intervals, merge all overlapping intervals. For exampl ...

  8. [LeetCode] 56 - Merge Intervals 合并区间

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

  9. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  10. 【leetcode】 Merge Intervals

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

随机推荐

  1. Android --自定义简单Toast

    1. 效果图

  2. FMS直播流发布时 Microphone Speex 编码设置注意事项

    1.为何要用 Speex?FP的默认音频编码是 NellyMoser,而FP10之后加入了 Speex.实际应用中,用默认的 NellyMoser 编码音频,会有个很大的问题,就是无法控制流码率浮动. ...

  3. 延迟加载图片插件LazyLoad.js的使用方法

    我们常常会见到很多网页的图片并不是一打开页面就全部加载的,而是浏览到当前的图片位置才显示出来.这是怎么实现出来的呢? 其实这就是目前较为流行的“延迟加载”(Lazy Load)技术,灵感来自Matt ...

  4. WebApp之Meta标签

    <meta name="apple-touch-fullscreen" content="yes">"添加到主屏幕“后,全屏显示 < ...

  5. linux 有趣的命令

    input: echo ' > ..XXX. .XXX.. > .XXXXY.TXXXX. > XXXXXYXTXXXXX > .VXXVYXTVXXX. > `.TYX ...

  6. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  7. 关于JS的算法

    一.快速排序 function qSort(arr) { if(arr.length === 0) { return []; } var left = []; var right = []; var ...

  8. QQ中打开链接不是默认浏览器

    电脑上装了搜狗和Chrome,Chrome为默认浏览器.但QQ中不论点什么都是以搜狗打开,解决办法: 1.设置, 2. 安全设置-->安全推荐-->使用搜狗打开链接增强安全性.去掉勾勾就行 ...

  9. SQL server 链接查询

    一.链接查询 1.join  on  左右链接 2.左右查询 left  right 3.union  上下链接

  10. DOS运行命令

    运行命令主要是DOS操作系统的运行方式.DOS时代的时候,为了方便用户的操作,微软公司将一些常用的命令,如DIR,CD等命令全部集成在系统里面. 基本定义 对于DOS来说是一个很大的优点.而存放这些内 ...