Start streaks at run heads
transcript
Parcel numbers arrive in any order. Some are scanned twice. Find the longest run of consecutive numbers. Where should you start? The start rule decides the cost. Here it is.
Put every number in a set. Presence is a count of one. From a start, look up the next number in the set. Keep walking while the number is present. Count the steps.
Start at every number and walk the run. Long runs get walked again and again. For the numbers one to 100,000 that is five billion steps. Start only at run heads. Each number is then walked once, a hundred thousand steps.
Check the number before each start. If it is present, this is not a head. Skip it, because a longer run already covers it. If it is missing, walk from here and count the run.
A set is a count of one. It answers presence. Skip every number that has a predecessor. Each number is walked once, so the cost is linear. Full breakdown in the lecture.