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. ZOJ 3703 Happy Programming Contest(DP)

    题目链接 输出路径,搞了一个DFS出来,主要是这里,浪费了好长时间. #include <cstdio> #include <string> #include <cstr ...

  2. 《你必须知道的.NET》书中对OCP(开放封闭)原则的阐述

    开放封闭原则(OCP,Open Closed Principle)是面向对象原则的核心.由于软件设计本身所追求的墓边就是封装变化,降低耦合,而开放封闭原则就是对这一目标的直接体现.(你必须知道的.NE ...

  3. [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

    Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...

  4. [CareerCup] 16.3 Dining Philosophers 哲学家聚餐问题

    16.3 In the famous dining philosophers problem, a bunch of philosophers are sitting around a circula ...

  5. Hibernate+Struts2进行数据的修改

    1.先把userid传给修改的页面 2.跳转到修改的页面 3.用request接收传入输入需改信息的页面 传到action Action,  通过request获取id service层 DAO层 & ...

  6. HTML静态网页 图片热点、框架、表单

    图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 框架: 1.frames ...

  7. linux笔记五-------编辑器

    1. 三种模式    命令(默认).尾行.编辑模式 2. 尾行模式    :    :q      退出vi编辑器    :w      保存修改    :wq     保存并退出编辑    :q!  ...

  8. phpwind < v6 版本命令执行漏洞

    phpwind/sort.php 会定期每天处理一次帖子的浏览量.回复量.精华版排序 代码直接使用savearray将数据库查询出来的内容写入php文件,savearray出来的参数,都使用" ...

  9. spring security 图解过滤器的使用

    1. HttpSessionContextIntegrationFilter 位于过滤器顶端,第一个起作用的过滤器. 用途一,在执行其他过滤器之前,率先判断用户的session中是否已经存在一个Sec ...

  10. javascript中字符串的常用方法

    <script type="text/javascript"> String substring(start,end): slice(start,end); 这两个函数 ...