-
Notifications
You must be signed in to change notification settings - Fork 3
/
q0435.py
32 lines (29 loc) · 996 Bytes
/
q0435.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python3
from typing import List
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
length = len(intervals)
if length < 2:
return 0
intervals.sort(key=lambda x: x[1])
result = 1
end = intervals[0][1]
for i in range(1,length):
if intervals[i][0] >= end:
result += 1
end = intervals[i][1]
return length - result
def eraseOverlapIntervals2(self, intervals: List[List[int]]) -> int:
length = len(intervals)
intervals.sort(key=lambda x: x[1])
result = 0
while len(intervals) > 0:
end = intervals[0][1]
result += 1
intervals.pop(0)
index = len(intervals) - 1
while index >= 0:
if intervals[index][0] < end <= intervals[index][1]:
intervals.pop(index)
index -= 1
return length - result