One missing delete. No answer.
transcript
This window looks right. It never finishes. One line is missing. Can you find it?
Find the longest stretch with at most two distinct values. Five, eight, five. Two values. Length three. Nine joins. Now the map holds three keys. So shrink from the left until two keys remain.
Five leaves. Its count drops to one. Eight leaves. Its count drops to zero. But the key stays in the map. Still three keys. So the loop keeps going. Five leaves. Nine leaves. Left runs past the end. The code crashes.
The map size is the distinct count. A key at zero lies about it. Delete the key when its count reaches zero. Then the shrink stops after eight leaves. Answer: three.
The shrink stops when one kind is gone completely. Gone means its key leaves the map too. Miss that line, and the window never becomes valid.
A count of zero means the value is gone. Make the map agree.
Full breakdown in the lecture.