Non-overlapping Intervals

update Aug 11,2017 21:40

LeetCodearrow-up-right

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.

Basic Idea:

我们可以尝试基于end的贪心算法。

  • 首先,我们对 intervals 排序,key = interval.end;

  • 然后,扫描每个interval,维持一个currEnd。如果当前interval的 start < currEnd,说明当前interval和之前某个或者某几个发生了重叠,我们此时贪婪地抛弃当前interval (count++),然后查看下一个。如果当前interval的 start >= currEnd,我们令 currEnd = interval.end,然后继续。

Java Code:

Python Code:

update Nov 18, 2018

Update: 对start排序的解法

如果不使用贪心算法,我们仍然有办法。首先仍然是对intervals按照start排序,然后从左到右扫描,每次比较 prev.endcurr.start,看是否有overlap,如果有,则移除end更加靠后的那个。