-
Notifications
You must be signed in to change notification settings - Fork 0
/
#0435 Non-overlapping Intervals.py
26 lines (22 loc) · 1.16 KB
/
#0435 Non-overlapping Intervals.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
from typing import List
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# Step 1: Sort the intervals based on their starting times.
intervals = sorted(intervals, key=lambda x: x[0])
# Step 2: Initialize the count of intervals to remove and the end of the last added interval.
res = 0
prev_end = intervals[0][1]
# Step 3: Iterate over the intervals starting from the second one.
for start, end in intervals[1:]:
# Step 4: If the current interval does not overlap with the previous one
if start >= prev_end:
# Update the end to the end of the current interval.
prev_end = end
else:
# If overlapping, increment the removal count.
res += 1
# Update the end to the minimum of the current interval's end and the previous end.
# This ensures that we consider the interval with the earliest end to minimize overlap.
prev_end = min(end, prev_end)
# Step 5: Return the total number of intervals to remove.
return res