Sort, then carry one number
transcript
A store lists its sale windows. Some lie fully inside longer ones. Checking every pair is slow. Sort first.
Sort the windows by start. Then equal starts need a rule. Carry one number: the furthest end so far. Compare each window with it once. A window that ends before it is covered.
Sorted by start: one to four, two to eight, three to six. Keep one to four. The furthest end is four. Two to eight ends beyond four. Keep it. Three to six ends before eight. Covered.
Two windows may start together. If the short one comes first, both are kept. Put the long one first. Then the short one is covered.
A hundred thousand windows make ten billion pair checks. After sorting, one pass is enough.
Longer range first, among equal starts. Keep a window when its end passes the furthest end. One pass, one number.
Sort by start. Carry the furthest end. Full breakdown in the lecture.