[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.

 

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

 

解法一:

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

 

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

 

解法二:

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

 

类似题目:

Find Right Interval

Data Stream as Disjoint Intervals 

Insert Interval

Merge Intervals

Maximum Length of Pair Chain

 

参考资料:

https://discuss.leetcode.com/topic/65629/concise-c-solution

https://discuss.leetcode.com/topic/65583/o-nlogn-java-solution

 

    原文作者:Grandyang
    原文地址: http://www.cnblogs.com/grandyang/p/6017505.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞