Non-overlapping Intervals
Return the minimum number of intervals to remove so that the rest are non-overlapping.
Open on LeetCode ↗Intuition
Almost everyone sorts by start time first, and it fails — one long interval beginning early crowds out several short ones that would all have fit. Sort by end instead. Removing the fewest is the same as keeping the most, which makes this classic activity selection: whichever interval finishes earliest leaves the most room for everything after it, so keeping it never costs you a better answer. Then greedily keep any interval starting at or after the last kept end.
Approach
Flip the objective
Minimising removals is maximising the size of a non-overlapping subset. Stated that way it is the meeting-rooms / activity-selection problem, which has a known greedy solution — recognising the equivalence is most of the work.
Sort by end time, not start
Sorting by start fails: one very long interval starting early can crowd out several short ones. Sorting by end works because among any set of conflicting intervals, keeping the one that finishes earliest dominates — it leaves a superset of the room any other choice would leave, so no optimal solution is ever lost.
Sweep and count conflicts
Track lastEnd, the end of the most recently kept interval. If the next interval starts at or after it, keep it and update lastEnd. Otherwise it clashes — increment the removal count and, crucially, drop this interval rather than the kept one, since the kept one ends no later and is therefore never the worse choice. O(n log n) for the sort, O(n) for the sweep.
Solution & live demo
Edge cases
Every interval is kept and 0 is returned.
One is kept and the remaining n - 1 are removed.
Not an overlap under this problem's definition, so the >= comparison keeps both.
Nothing to remove; the answer is 0.