Problem
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.
Solutionclass Solution { public int eraseOverlapIntervals(Interval[] intervals) { if (intervals == null || intervals.length == 0) return 0; //find non-overlapping count, so sort by end Arrays.sort(intervals, (a, b)->(a.end-b.end)); int end = intervals[0].end; int count = 1; for (int i = 1; i < intervals.length; i++) { if (intervals[i].start >= end) { end = intervals[i].end; count++; } } return intervals.length-count; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72267.html
Problem Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times....
Problem We are given a list schedule of employees, which represents the working time for each employee. Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order. ...
摘要:问题描述分析这道题的关键在于理解问题,抽取原型,理解中间可以部分如何界定,以及非部分如何进行追加。需要注意的是循环到最后一个元素和在最后一个元素的区别。 问题描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...
摘要:我们只要把所有和该有重叠的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表连起来,就是结果了。 Merge Intervals 最新更新请见 https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...
摘要:题目要求给定一组顺序排列且相互之间没有重叠的区间,输入一个区间,将它插入到当前的区间数组中,并且将需要合并的区间合并,之后返回插入并且合并后的区间。我们将这三个类型的区间分别标注为类型,类型,类型。 题目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge...
阅读 849·2021-11-22 09:34
阅读 931·2021-10-08 10:16
阅读 1753·2021-07-25 21:42
阅读 1776·2019-08-30 15:53
阅读 3488·2019-08-30 13:08
阅读 2128·2019-08-29 17:30
阅读 3258·2019-08-29 17:22
阅读 2154·2019-08-29 15:35