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 ...
随机推荐
- SQLSERVER远程备份、恢复(转)
SQLSERVER服务实例名称:192.168.0.2需要备份的数据库名称: a备份机器名称(Client端):192.168.0.3备份机用户:zf 密码:123备份机域名:domain备份机提供备 ...
- java eclipse中的代码联动提示功能
eclipse中的代码联动提示设置:window--->preferences--->java--->editor----> content assist的auto activ ...
- oracle免安装客户端设置
对oracle不是很熟悉,就是使用层面的,开发时往往需要连接oracle,又不想单独安装,一般都用个免安装的客户端罢了,再次记录一下自用 1.下载oracle免安装的客户端 下载地址:http://w ...
- 用css3实现一个带缺口的圆圈(图)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [LintCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 练习一:SQLite基本操作
一.基础知识: 运用场景: 1>应用运行需要保存一系列有一定关系有一定结构的数据(文本也可以但是存储效率低) 2>文件类型:.db(一个数据库就是一个.db文件) 3>路径:/dat ...
- 使用explain查看mysql查询执行计划
explain语句: 字段解释: type: all(全表扫描) ref() possible_keys: 预测使用什么列做为索引 key: 实际使用的key ...
- HTML元素事件说明
blur( ) 元素失去焦点 a, input, textarea, button, select, label, map, area change( ) 用户改变域的内容 input, textar ...
- BizTalk开发系列(二十四) BizTalk项目框架建议
Asp.NET有MVC框架,大部份的开发都是按照MVC进行的.BizTalk是面向消息的开发,不能完全采用分层的开发模式.而微软只提供了 BizTalk项目开发的基本策略,通过分析相关的Complex ...
- request获取请求头和请求数据
package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...