GreedyAnimated
Non-overlapping Intervals
Remove the fewest intervals by keeping those that end earliest.
Time
O(n log n)
Extra space
O(1)
Use it for
Interval cleanup.
How it works
- To remove as few intervals as possible, keep as many non-overlapping ones as possible.
- That is exactly activity selection: sort by end, keep each interval that starts at or after the previous kept end.
- Everything not kept must be removed. Intervals that only touch (end = next start) do not overlap.
Watch it run
Loading animation…