Intervals - Non-overlapping Intervals (Max Non-Overlap)
Consider the following Python function that returns the maximum number of non-overlapping intervals. What is the value of the variable
removals after the second iteration of the loop when the input is [[1,3],[2,4],[3,5]]?
from typing import List
def max_non_overlapping_intervals(intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[0])
removals = 0
last_end = intervals[0][1]
for i in range(1, len(intervals)):
if intervals[i][0] < last_end:
removals += 1
last_end = min(last_end, intervals[i][1])
else:
last_end = intervals[i][1]
return len(intervals) - removals
intervals = [[1,3],[2,4],[3,5]]
print(max_non_overlapping_intervals(intervals))