Skip to content

Commit e579568

Browse files
committed
solve: non overlapping intervals
1 parent 27e4c4c commit e579568

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

non-overlapping-intervals/evan.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
6+
if not intervals:
7+
return 0
8+
9+
# Sort the intervals based on their end times
10+
# to include as many meetings as possible
11+
intervals.sort(key=lambda x: x[1])
12+
13+
# the count of non-overlapping intervals
14+
count = 0
15+
# the end time of the last added interval
16+
end = float("-inf")
17+
18+
for interval in intervals:
19+
if interval[0] >= end:
20+
# If the current interval does not overlap with the last added interval, include it
21+
end = interval[1]
22+
else:
23+
# Otherwise, increment the count of intervals to be removed
24+
count += 1
25+
26+
return count

0 commit comments

Comments
 (0)