【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.
解题思路:本题可以用贪心算法。 首先对 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的更多相关文章
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【leetcode】1288. Remove Covered Intervals
题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- C#基础提升系列——C# 泛型
C# 泛型(Generics) 泛型概述 泛型是C#编程语言的一部分,它与程序集中的IL(Intermediate Language,中间语言)代码紧密的集成.通过泛型,我们不必给不同的类型编写功能相 ...
- python的main函数
代码示例:test.py import sys if __name__ == '__main__' size = len(sys.argv) p1 = sys.argv[] p2 = sys.argv ...
- Ubuntu Visual code安装与使用
1.直接启动软件中心,输入visual studio code,点击install即可,千万千万不要去装逼搞什么linux指令安装,死都不知道怎么死的 2.Visual code是以文件夹为工程目录的 ...
- SSH小应用
1:Spring整合Hibernate <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hi ...
- 环境变量(windows下tomcat问题);shh连接虚拟机网络配置
环境变量(windows下tomcat问题) 有tomcat有jdk 再配置环境变量:参考 提示:若选择“用户变量”,则本次配置的变量只对该用户有效 若选择“系统变量”,则对所有用户 ...
- Divideing Jewels
Divideing Jewels 时间限制: 1 Sec 内存限制: 128 MB提交: 63 解决: 17[提交][状态] 题目描述 Mary and Rose own a collection ...
- Castle动态代理拦截
比如现在有一个方法,进行积分奖励 PointAdd 在不改变原来方法的基础上,增加积分奖励的日志 using Castle.DynamicProxy; public class AuditTraceI ...
- CentOS 7在VMware 12中共享文件看不见的问题?
前言 由于rhel 7.2因为没有注册导致yum无法使用,包括自己配置本地源,这个命令在你没有注册都不能使用,每次使用rpm去装软件,自己去找缺少的依赖包,实在是麻烦.于是不如就换一个系统,CentO ...
- ceph安装过程
创建群集[2019-03-20 18:35:04,232][ceph_deploy.conf][DEBUG ] found configuration file at: /home/sceph/.ce ...
- 测开之路五十五:实现类似于unittest查找case
实现给一个路径,去查找test开头的测试用例文件 创建一个计算器的类,方便后面测试用 class Calculator(object): def add(self, x, y): return x + ...