Given an array
intervals
whereintervals[i] = [li, ri]
represent the interval[li, ri)
, remove all intervals that are covered by another interval in the list.The interval
[a, b)
is covered by the interval[c, d)
if and only ifc <= a
andb <= d
.Return the number of remaining intervals.
class Solution:
def removeCoveredIntervals(self, intervals):
# 排序:按照 each[0] 从小到大,相同时按照 each[1] 从大到小
intervals = sorted(intervals, key=lambda x:x[1], reverse=True)
intervals = sorted(intervals, key=lambda x:x[0])
end = intervals[0][1] - 1
count = 0
for each in intervals:
# each[1] 包含不了后面的 interval
if each[1] > end:
count += 1
end = each[1]
return count