Leetcode: Non-overlapping Intervals
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的更多相关文章
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [Leetcode Week2]Merge Intervals
Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Java for LeetCode 056 Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- leetcode[55] Merge Intervals
题目:给定一连串的区间,要求输出不重叠的区间. Given a collection of intervals, merge all overlapping intervals. For exampl ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
随机推荐
- 【HDU】1536 S-Nim
http://acm.hdu.edu.cn/showproblem.php?pid=1536 题意:同nim...多堆多询问...单堆n<=10000,每次取的是给定集合的数= = #inclu ...
- Golang redigo hmset hset 问题
最近公司项目,换到了golang 下面来开发,遇到了redis存储链表的问题,困扰了我好几天,后面静下心来,好好读了一下源码,发现官方的例子,最终还是羊毛出在羊身上 c, err := dial() ...
- Find a way——L
L. Find a way Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave N ...
- 如何给Sublime安装插件
第一步:点击链接http://sublime.wbond.net/Package%20Control.sublime-package下载Package Control. 第二步:点击打开Sublime ...
- centos安装php
1.安装phpyum install php php-devel重启apache使php生效 2.此时可以在目录:/var/www/html/下建立一个PHP文件代码:<?php phpinfo ...
- Hibernate的一级二级缓存机制配置与测试
特别感谢http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session ...
- 64位虚拟机安装64位ubuntu出现问题
virtualbox 出现this kernel requires an an x86-64 cpu 错误 如题,但是主机是win8 64位,使用virtualbox安装ubuntu-12.04.3- ...
- pt-table-checksum使用实践
在工作中接触最多的就是mysql replication,由于现在公司也还在使用mysql 5.1.x版本,在复制方面还是比较多的问题,比如主库宕机或者从库宕机都会导致复制中断,通常我们需要进行人为修 ...
- smartlink
链接:https://www.zhihu.com/question/21783165/answer/20323202 Wi-Fi本身是属于固定频段上的TDD通讯机制,目前尚用的也就是2.4G和5.8G ...
- iOS CUICatalog: Invalid asset name supplied: (null)
iOS开发出现CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000 原因: 你用了这个方 ...