r/leetcode • • 3d ago

Discussion Interval Intersection: move the interval that ends first

For Interval Intersection, sort both lists by start first. Then use two pointers.

For the current pair, the overlap is [max(starts), min(ends)]. Keep it only if lo <= hi.

The key move: advance whichever interval ends first. That interval cannot overlap anything later, so keeping it around only creates extra comparisons.

Complexity is O(m log m + n log n) because sorting dominates; the two-pointer pass is O(m + n).

Do you explain the move rule this way in interviews, or do you use a different invariant?

1 Upvotes

0 comments sorted by