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:

  1. You may assume the interval's end point is always bigger than its start point.
  2. 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的更多相关文章

  1. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  2. 【一天一道LeetCode】#56. Merge Intervals

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  3. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode OJ 56. Merge Intervals

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

  5. [leetcode sort]56. Merge Intervals

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

  6. 【LeetCode】56. Merge Intervals

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

  7. LeetCode OJ:Merge Intervals(合并区间)

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

  8. leetcode || 56、 Merge Intervals

    problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3], ...

  9. [LeetCode] 435. Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

随机推荐

  1. FastJSON 使用

    FastJSON是一个Java语言编写的高性能,功能完善,完全支持http://json.org的标准的JSON库.多了不说了,百度一下一大把. 在此,简单的总结一下自己用过,测试过的方法. 如果使用 ...

  2. WIN32API 自定义颜色下拉列表控件

    效果如下: 原创-转载请著名来源 1,新建颜色属性类“CNColor”: class CNColor { public: COLORREF m_crColor; //颜色RGB值 WCHAR m_cC ...

  3. ios面试技巧

    一.软件开发流程? 软件设计思路和方法的一般过程,包括设计软件的功能和实现的算法和方法.软件的总体结构设计和模块设计.编程和调试.程序联调和测试以及编写.提交程序. 1 相关系统分析员和用户初步了解需 ...

  4. MongoDB数据导入导出成csv或者json

    1. 从远程数据表拉取数据到本地json文件 mongoexport --host 远程服务器IP --port 远程服务器端口 --username 远程数据库用户名 --password 远程数据 ...

  5. KVO

    •基本概念 Key Value Observing, 键值观察者.它提供一种机制,当指定的对象的属性被修改后,则对象就会接收到通知. 与NSNotification不同,键值观察中并没有中心对象来为所 ...

  6. matlab 连续读取多个文件

    方法1: 把文件的文件名按一定的规律命名,假如:filename1.txt,filename2.txt,...,fielname100.txt,在读取的时候则可以使用循环: for i = 1:100 ...

  7. 制作Nine-Patch图片的流程

    1.找到draw9patch.bat文件,在Android sdk目录下的tools文件夹中. 2.双击打开draw9patch.bat文件,在导航栏点击File->Open 9-patch将图 ...

  8. [z]查表空间使用情况

    SELECT UPPER(F.TABLESPACE_NAME) "表空间名",D.TOT_GROOTTE_MB "表空间大小(M)",D.TOT_GROOTTE ...

  9. Android SDK更新以及ADT更新出现问题的解决办法

    http://jingyan.baidu.com/article/148a192196209d4d70c3b168.html

  10. H5学习系列之Communication API

    1 .postMessage API 首先介绍一下什么是iframe? 百度百科里这样写道:IFRAME,HTML标签,作用是文档中的文档,或者浮动的框架(FRAME). 我的理解就是网页中的网页. ...