CodeArchon

mediumIntervalsAmazon

Lecture Hall Pruner

An admin removes the FEWEST sessions from a conflict-ridden schedule so that the remainder is conflict-free.

Given intervals, return the minimum number of intervals to remove so no two overlap.

Choose approach

1/5
Narrationstep 1 of 5

Keep I0; running end = 2.

1def eraseOverlapIntervals(intervals):
2 intervals.sort(key=lambda x: x[1])
3 end, erased = float('-inf'), 0
4 for s, e in intervals:
5 if s < end: erased += 1
6 else: end = e
7 return erased
Variables
i=0
end=2

How to think about it

  1. Equivalent to maximum non-overlap set.
  2. Sorting by end > sorting by start for this task.

Edge cases to catch in interview

  • All overlap -> keep 1, remove n-1.
  • All disjoint -> 0 removals.

Examples

Example 12
{"intervals":[[1,2],[1,2],[1,2]]}

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed