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

  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(vectorint>>& intervals) {
        int res = 0, n = intervals.size(), last = 0;
        sort(intervals.begin(), intervals.end());
        for (int i = 1; i < n; ++i) {
            if (intervals[i][0] < intervals[last][1]) {
                ++res;
                if (intervals[i][1] < intervals[last][1]) last = i;
            } else {
                last = i;
            }
        }
        return res;
    }
};

 

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

 

解法二:

class Solution {
public:
    int eraseOverlapIntervals(vectorint>>& intervals) {
        if (intervals.empty()) return 0;      
        sort(intervals.begin(), intervals.end());
        int res = 0, n = intervals.size(), endLast = intervals[0][1];
        for (int i = 1; i < n; ++i) {
            int t = endLast > intervals[i][0] ? 1 : 0;
            endLast = t == 1 ? min(endLast, intervals[i][1]) : intervals[i][1];
            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 题目讲解汇总(持续更新中...)

转载于:https://www.cnblogs.com/grandyang/p/6017505.html

你可能感兴趣的:([LeetCode] 435. Non-overlapping Intervals 非重叠区间)