Drop on arrival
transcript
Some problems keep a list of candidates. Most candidates can never win again. Drop them the moment they lose. Here is how it looks on a belt.
A new item arrives at the end. Remove every entry that it beats for good. The entries that remain stay in order.
Four goes on the stack. Minus one is lighter, so it is destroyed. Five goes on the stack above four. Minus six is heavier than five and four. Both leave the stack. Only minus six remains on the belt.
The front holds the oldest candidate. The back holds the newest candidate. So the best answer is always one glance away.
Each item is pushed exactly once. And it is popped at most once. So the pass is linear. That is why such a loop can be trusted on large inputs.
Say what makes an entry useless. Drop it the moment a new item arrives. Full breakdown in the lecture.