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. VS 2008 快捷键

    注释代码:<Ctrl+K,C>取消注释:<Ctrl+K,U> 封装字段(生成get.set方法): <Ctrl+R,E> 定位大括号范围:光标放在其中一个括号的位置 ...

  2. docker安装

    系统要求:需要一个64位的centos7操作系统和版本3.10或更高版本的Linux内核 开始安装: uname -r   //查看内核版本yum -y update //更新系统更新到最新 #安装d ...

  3. H5表单中placeholder属性的字体颜色问题

    最近做项目的时候遇到的一些小样式问题,有关表单.并且在接下来几天的面试人中五个人都没有回答上来,改变placeholder属性的默认字体颜色,感觉有必要总结一下. 如何改变默认字体的颜色? @blue ...

  4. JQUERY 保存成功后又下角动态提示

    $.messager.show({ // show error message         title : '操作结果',         msg : '操作成功!'        });

  5. Convert.ToInt32,int.Parse,int.TryParse,(int)的区别

    1 (int)变量名[强制类型转换] 该转换方式主要用于数字类型转换,从int类型到long,float,double,decimal类型,可以使用隐式转换,但是从long类型到int类型就需要使用显 ...

  6. JS产生随机数

    <script>   function GetRandomNum(Min,Max){   var Range = Max - Min;   var Rand = Math.random() ...

  7. CentOS 6.5 安装Oracle 11G R2问题列表

    1. 文章中写vi/etc/profile #这个很重要 if [ $USER ="oracle" ]; then     if [ $SHELL="/bin/ksh&q ...

  8. 使用AS编译jni文件无法编译出arm64-v8a,x86_64和mips64平台的.so文件的解决方法

    我用的插件版本是:classpath 'com.android.tools.build:gradle-experimental:0.4.0',AS集成和使用ndk编译项目参考官方demo:https: ...

  9. mysql行列调换方法

    行变列,列变行 财务样式模板: CREATE TABLE `grade` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR ...

  10. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...