题目如下:

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.

解题思路:本题可以用贪心算法。 首先对 intervals 按start从小到大排序,如果start相同,则按end从小到大。接下来遍历intervals,如果intervals相邻的两个元素有重叠,删除掉end较大的那个,最后intervals中留下来的元素都是不重叠的。

代码如下:

# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e class Solution(object):
def eraseOverlapIntervals(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
def cmpf(i1,i2):
if i1.start != i2.start:
return i1.start - i2.start
return i1.end - i2.end
intervals.sort(cmp=cmpf)
keep = None
res = 0
while len(intervals) > 0:
item = intervals.pop(0)
if keep == None or keep.end <= item.start:
keep = item
elif keep.end <= item.end:
res += 1
elif keep.end > item.end:
keep = item
res += 1
return res

【leetcode】435. Non-overlapping Intervals的更多相关文章

  1. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  2. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. 【leetcode】1288. Remove Covered Intervals

    题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...

  4. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  5. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  6. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. C#基础提升系列——C#文件和流

    C#文件和流 本文主要是对C#中的流进行详细讲解,关于C#中的文件操作,考虑到后期.net core跨平台,相关操作可能会发生很大变化,所以此处不对文件系统(包括目录.文件)过多的讲解,只会描述出在. ...

  2. leetcode-15双周赛-1286-字母组合迭代器

    题目描述: 方法: class CombinationIterator: def __init__(self, characters: str, combinationLength: int): se ...

  3. JMeter生成UUID方式

    1. 使用JMeter工具中自带的函数__UUID 2. 使用Beanshell组件,在脚本中引入java.util.UUID,通过java来生成 import java.util.UUID; UUI ...

  4. 六、unique_lock

    一.unique_lock取代lock_guard 是个类模板,一般用lock_guard,unique_guard更灵活,效率差一点,内存占用多了一点. 二.unique_lock 的第二个参数 1 ...

  5. 2019-05-16 Ubuntu使用

    Ubuntu的基本操作 查看操作系统版本 https://www.hostingadvice.com/how-to/ubuntu-show-version/ clu@sha01vmdev08:~/so ...

  6. PAT 2019-3 7-3 Telefraud Detection

    Description: Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, ...

  7. 原生js事件绑定

    一.JS事件 (一)JS事件分类 1.鼠标事件: click/dbclick/mouseover/mouseout 2.HTML事件: onload/onunload/onsubmit/onresiz ...

  8. [CF1228] 简要题解

    A 题意 求\(l \le x \le r\)的所有数位不同的数\(x\), 任意输出一个. \(1 \leq l \leq r \leq 10 ^5\) Solution 按照题意模拟即可. #in ...

  9. sublime text3中使用PHP编译系统

    前言: php是服务器端语言,我们平时写的php代码想要查看运行结果的话,通常会搭建web服务器,然后通过浏览器访问.而对于有时候一些简单的测试代码来说,此过程就有点繁琐了.编译系统的好处是,可以让我 ...

  10. es6 Promise.reject()方法

    es6 Promise.reject()方法:https://blog.csdn.net/ixygj197875/article/details/79188195