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. JS逻辑运算符&&与||的短路运算

    最近看到一个360面试题,题目如下: 下面代码的输出值是? alert(1&&2); 正确的结果是 2. 1.后来仔细研究了一下JS逻辑运算的相关内容,在MDN上面找到相应描述: 下面 ...

  2. JSP(include指令)页面

    <%@ page language= "java" contentType="text/html;charset=UTF-8" %><html ...

  3. PCIE学习

    PCIe在传输中用8b/10b编码,所以单PCEe2.0的有效带度是4Gb/s x2模式将用于内部接口而非插槽模式 PCIe卡能使用在至少与之传输通道相当的插槽上(例如x1接口的卡也能工作在x4或x1 ...

  4. ViewFlipper(翻转视图)的使用

    android developers java.lang.Object --android.view.View ----android.view.ViewGroup ------android.wid ...

  5. allegro - 层叠相关参数

      层叠结构设置 弹出Layout Cross Section对话框 Subclass Name一列是该层的名称,可以按照自己的需要来填写.Type 列选择该层的类型,有三种: ·CONDUCTOR: ...

  6. HTML5 ---localStorage储存实例

     <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...

  7. 第三节 Hello world --python初体验

    祭旗--hello world 据说简单优雅.功能强大是python的魅力所在,这里看到简单了,优雅是什么样的,接下来的学习中慢慢体会吧! print ("Hello world" ...

  8. 什么是遗传方差(Genetic variance)、加性遗传方差(Additive genetic variance)、显性遗传方差(Dominance genetic variance)、上位遗传方差(Epistatic genetic variance)

    遗传方差:遗传方差又称表型方差(phenotypic variance),通常结合基因型方差(genotype variance)和环境方差(environmental variance).遗传方差主 ...

  9. Haproxy+Keepalived高可用负载均衡详细配置

    本文所使用的环境: 10.6.2.128    centos6.5 10.6.2.129    centos6.5 VIP  为10.6.2.150 要实现的目标: 实现10.6.2.128和10.6 ...

  10. Java程序调用javascript等脚本的实现方法

    public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodEx ...