[LeetCode] 435 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.
这个题目和 http://www.cnblogs.com/javanerd/p/6068552.html 这道题目差不多,都可以对一个interval线段数组进行排序,然后用滑动窗口来解。
但是,因为涉及到一些比较复杂的条件判断,所以排序以后,直接用了双层循环去两两比较,同时用一个boolean数组记录出已经被踢出去的线段,用来提高效率。
代码如下:
public int eraseOverlapIntervals(Interval[] intervals) {
if (intervals.length == 0 || intervals.length == 1) {
return 0;
} else {
int result = 0;
int[] mark = new int[intervals.length];
Arrays.fill(mark, 0);
Arrays.sort(intervals, (o1, o2) -> {
if (o1.start == o2.start) {
return o2.end - o1.end;
} else {
return o1.start - o2.start;
}
}); //按照start从小到大,然后end从大到小排序.
for (int i = 0; i < intervals.length - 1; i++) {
if (mark[i] != 1) {
for (int j = i + 1; j < intervals.length; j++) {
if (mark[j] == 1) {
continue;
} else {
Interval left = intervals[i];
Interval right = intervals[j];
if (left.start == right.start) { //如果两个线段start一样,那么删掉end比较大的那个.
mark[i] = 1;
result++;
break;
} else if (left.end > right.start) { //如果两个线段有折叠
result++;
if (left.end <= right.end) { //如果右边的线段的end比较大,那么删掉右边线段,同时往后移动一位,继续比较下一个.
mark[j] = 1;
} else {
mark[i] = 1; //如果左边的线段的end比较大,那么删掉左边的.同时结束内存循环.
break;
}
} else { // left.end <= right.start,因为已经排序了,那么后面的start必然都比left.end大,提前终止循环
break;
}
}
}
}
}
return result;
}
}
[LeetCode] 435 Non-overlapping Intervals的更多相关文章
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- 【一天一道LeetCode】#56. Merge Intervals
一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ 56. Merge Intervals
题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...
- [leetcode sort]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
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
- leetcode || 56、 Merge Intervals
problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3], ...
- [LeetCode] 435. Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
随机推荐
- eclipse中的 Compiler compliance level含义
The compiler compliance setting tells the compiler to pretend it's a different version of Java. The ...
- jsp js action之间的传值
1.struts2 action如何向JSP的JS函数传值 action中定义变量public class TestAction extends ActionSupport implements Se ...
- dataguru(炼数成金)大数据培训基地印象
dataguru访问地址:http://f.dataguru.cn/?fromuid=99611 课程优惠码:C4B6 这段时间一直在dataguru(炼数成金)上学习<hadoop数据分析平 ...
- jq 部分用法
这几天一直在写前台,因为jq是在客服端处理数据的,所以公司,一般都用这种方法,下面是我这几天用到的一些东西 1.修改table表格的第一轮显示值 function changeTableRowValu ...
- .net WebClient发送请求实例:
public static Main(string [] ager) { WebClient client = new WebClient(); client.Headers.Clear(); cli ...
- iOS8 定位补充
iOS 8定位补充 iOS 8定位需要修改2个地方 1.info.plist文件中添加NSLocationAlwaysUsageDescription:描述信息 从iOS 8开始,用户定位分两种情况 ...
- MVC4 本地正常运行,发布到IIS7->403 - 禁止访问: 访问被拒绝。
代码编写完成,计划发布一个版本测试,没想到发布到IIS7 竟然报错“403-禁止访问”.还真第一次遇到这种问题..... 折腾了半天,终于解决. 1.提示报错403: 禁止访问: 访问被拒绝.您无权使 ...
- 简单的通用TreeView(WPF)
工作中要为很多类创建TreeView, 很多时候仅仅是因为要显示字段不同, 就得Ctrl+C.Ctrl+V复制一份几乎相同的代码, 这难免让人生厌, 于是希望像泛型集合的扩展方法那样, 可以在使用的时 ...
- 清空IE缓存
1.打开IE Internet选项 点击设置 2.打开临时文件 点击 查看文件 将目录下的 文件全部删除 重新打开网站即可 到此IE缓存就被删除.
- [问题记录.VisualStudio]VS2013无法新增和打开项目
[问题描述] 1) 打开项目失败,报“项目文件只读”或“空引用”错误. 2) 无法新建项目,没有任何可用模板. 3) TFS都正常 [问题产生] 机器环境: 1) 装的Win10双系统,其中一个系统是 ...