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:


Example 2:

Input: [ [1,2], [1,2], [1,2] ] Output: 2 Explanation:


Example 3:

Input: [ [1,2], [2,3] ] Output: 0 Explanation:


笨啊,思考了一会发现不会,so参考了​​其他人的思路​​​
按end排序,用一个变量记录当前end的最大值,如果后面的interval能和前面的interval兼容,则加入集合,并扩大end的最大值,否则不兼容,计数加一。
程序还算好写,用了一个lambda表达式,和sort排序,Python代码:

# Definition for an interval.
class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e

class Solution(object):
def eraseOverlapIntervals(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
if not intervals:
return 0

intervals.sort(key = lambda i : i.end)
count = 0
end = intervals[0].end
length = len(intervals)
for i in range(1, length):
if intervals[i].start >= end:
end = intervals[i].end
else :
count += 1
return count

# arr = [ [1,2], [2,3], [3,4], [1,3] ]
arr = [ [1,2], [2,3] ]
intervals = []
for i in arr:
intervals.append(Interval(i[0], i[1]))

r = Solution().eraseOverlapIntervals(intervals)
print(r)