- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/non-overlapping-intervals/description/
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、 Youmayassumetheinterval'sendpointisalwaysbiggerthanitsstartpoint.;
2、 Intervalslike[1,2]and[2,3]haveborders"touching"buttheydon'toverlapeachother.;
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.
给出了一些区间,求最少需要移除多少个区间才能保证所有的区间不重叠。如果两个区间的起始点与终点重复,不认为是重叠。
看到区间最值题一般想到排序+贪心。这个题和452. Minimum Number of Arrows to Burst Balloonsopen in new window挺相似。
这个题的做法也不难,这么思考:我们尽量移除那些覆盖最广的区间。
先对所有区间的起点进行排序,然后进行遍历,如果新的区间起点比老的区间终点小的话,说明有重叠,需要移除区间,移除哪个区间呢?当然是终点最靠后的终点。
我们使用last指针指向上个保留下来的节点,如果intervals[i].end < intervals[last].end,则代表新的区间终点更靠前,所以使用第i个节点代表last,也就是说移除了上面的那个last。然后统计移除了多少次区间即可。
时间复杂度是O(nlogn + n),空间复杂度是O(1).
代码如下:
# 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
"""
if not intervals: return 0
intervals.sort(key = lambda x : x.start)
last = 0
res = 0
for i in range(1, len(intervals)):
if intervals[last].end > intervals[i].start:
if intervals[i].end < intervals[last].end:
last = i
res += 1
else:
last = i
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
参考资料:
http://www.cnblogs.com/grandyang/p/6017505.html
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
所以我正在做一个Java作业,我必须创建一个矩形类,该类在一个绘制重叠矩形的程序中使用,并且在矩形重叠的地方,用新的颜色绘制一个新的矩形。我添加了硬件描述的链接,因为我认为让您查看它比我试图解释它更容
如何将两个重叠的 div 加上第三个 div 放在那些重叠的 div 的右侧(但第三个 div 没有一直 float )? I overlap id=two. I overlap id=one. I
我需要你的帮助,我有一个问题(见图),我假设有两个数组,每个数组都包含不同长度和实际值的间隔,我需要找出我如何有效地重叠这些间隔。 我对想法、论文理论或具体算法持开放态度,它们会让我找到出路! 我猜想
尝试编写一个从聚合数据返回气泡图的函数。 我将“agg”中的 data.frame 列传递给它。 aggs2 9) } ##order data.frame by mean agg1 <
我正在使用 d3.js 以这种方式生成一些直接位于彼此上方的矩形: var greenRed = d3.select(".green-red").append("svg") .attr("he
我正在尝试创建一个库,它提供一个简单的链表实现以及该链表的一些概括,例如堆栈和队列,所有这些都基于基本链表。 问题是,我希望拥有具有自己的“私有(private)”函数的不同类型,这样您就不会使用“s
我使用 AJAX 和 JQuery 从 MySQL 数据库中提取数据,基本上使用以下内容: function getCard(card, lineNr, linedisplay, type){ var
如何在 SQL OVERLAPS 中包含开始日期和结束日期? 喜欢=。怎么办? 因为在我看来 OVERLAPS 只检查两者之间的范围,但是例如: 日期 2001 年 1 月 1 日 - 2001 年
我在使用在 Google Web Fonts 上找到的字体时遇到了一些问题. 正如您在下面发布的图片中看到的,当我使用 Firefox 时,“Versus”中的大写 V 与“e”重叠。尽管当我使用 C
题目地址:https://leetcode.com/problems/image-overlap/description/ 题目描述 Twoimages A and B are given, re
题目地址:https://leetcode.com/problems/rectangle-overlap/description/ 题目描述: Arectangle is represented
在我们的测试环境中,我们的 solr 搜索引擎一直存在许多问题。我们在 4.6 版、单分片、4 个节点上有一个 solr 云设置。我们看到领导节点上的 CPU 水平线达到 100% 几个小时,然后服务
我正在做一个 excel 任务,我必须找出是否有重叠的日期。 在我的 excel 工作表中有 startDate(column D) 和 EndDate(comun E) 的列,由此我必须确定是否存在
在这张图片中,一个全景项目的内容渗透到前一个项目上: 如何在 Expression Blend/with XAML 中执行此操作? 这是我目前的 XAML:
我已经以编程方式创建了一个 iPhone UI,但我只是不知道如何调整 View ,以免它被 TabBar 重叠。这是所有权层次结构: -AppDelegate UITabBarController
我正在使用 wxMaxima 16.12.0 (Maxima 5.39.0),当我尝试打印输出时,出现重叠文本。 这是一个例子(请忽略命令不正确的事实) 第一张图片 有时也会发生在 print和 pr
我尝试为 javascript 添加一些语法高亮到 vim,但我一直遇到一个问题:当字符已经高亮时,它们似乎被所有其他正则表达式完全忽略。 例如,我尝试为函数的参数列表添加语法高亮显示。在创建正确的
我有五个 ImageView(ImageButton),我想将其显示在一行上,但是当我在小型设备上时,我的最后一个图像被裁剪了? 我该如何修复它? 有没有办法检测屏幕宽度? |一个 |乙| C | d
我尝试为 javascript 添加一些语法高亮到 vim,但我一直遇到一个问题:当字符已经高亮时,它们似乎被所有其他正则表达式完全忽略。 例如,我尝试为函数的参数列表添加语法高亮显示。在创建正确的
我将用数学解释,这是我正在努力为以下内容编写 Scheme 代码的转换: (f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) c
我是一名优秀的程序员,十分优秀!