[LeetCode] 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:

  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.

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

 

这道题给了我们一堆区间,让求需要至少移除多少个区间才能使剩下的区间没有重叠,那么首先要给区间排序,根据每个区间的 start 来做升序排序,然后开始要查找重叠区间,判断方法是看如果前一个区间的 end 大于后一个区间的 start,那么一定是重复区间,此时结果 res 自增1,我们需要删除一个,那么此时究竟该删哪一个呢,为了保证总体去掉的区间数最小,我们去掉那个 end 值较大的区间,而在代码中,我们并没有真正的删掉某一个区间,而是用一个变量 last 指向上一个需要比较的区间,我们将 last 指向 end 值较小的那个区间;如果两个区间没有重叠,那么此时 last 指向当前区间,继续进行下一次遍历,参见代码如下:

解法一:

class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
int res = , n = intervals.size(), last = ;
sort(intervals.begin(), intervals.end());
for (int i = ; i < n; ++i) {
if (intervals[i][] < intervals[last][]) {
++res;
if (intervals[i][] < intervals[last][]) last = i;
} else {
last = i;
}
}
return res;
}
};

我们也可以对上面代码进行简化,主要利用三元操作符来代替 if 从句,参见代码如下:

解法二:

class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.empty()) return ;
sort(intervals.begin(), intervals.end());
int res = , n = intervals.size(), endLast = intervals[][];
for (int i = ; i < n; ++i) {
int t = endLast > intervals[i][] ? : ;
endLast = t == ? min(endLast, intervals[i][]) : intervals[i][];
res += t;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/435

类似题目:

Find Right Interval

Data Stream as Disjoint Intervals

Insert Interval

Merge Intervals

Maximum Length of Pair Chain

Minimum Number of Arrows to Burst Balloons

参考资料:

https://leetcode.com/problems/non-overlapping-intervals/

https://leetcode.com/problems/non-overlapping-intervals/discuss/91713/Java%3A-Least-is-Most

https://leetcode.com/problems/non-overlapping-intervals/discuss/91700/Concise-C%2B%2B-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

上一篇:输入一个正数 n,输出所有和为 n 连续正数序列。 java实现


下一篇:php部分--数组(包含指针思想遍历数组);