-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonOverlappingIntervals.py
36 lines (35 loc) · 1.18 KB
/
NonOverlappingIntervals.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
33
34
35
36
# https://leetcode.com/problems/non-overlapping-intervals/
# #greedy
class Solution:
class Point:
def __init__(self, start, end):
self.start = start
self.end = end
def __lt__(self, other):
if (self.start != other.start):
return self.start < other.start
else:
return self.end < other.end
def __repr__(self):
return "{} {}".format(self.start, self.end)
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
l = list()
for index, (start, end) in enumerate(intervals):
l.append(self.Point(start, end))
l.sort()
if len(l) == 0:
return 0
count = 0
max_overlap_point = l[0]
print(max_overlap_point)
for i in range(1, len(l)):
cur_point = l[i]
# check interval
if max_overlap_point.end >= cur_point.end:
count += 1
max_overlap_point = cur_point
elif max_overlap_point.end > cur_point.start:
count += 1
else:
max_overlap_point = cur_point
return count