The repeat trap
transcript
Two payments of fifty. Do they count twice? A plain heap says yes. Here is the fix.
The amounts are five and five. A plain heap fills both places. It answers five, too early. Only one different amount arrived. So the right answer is minus one.
Keep a set of the held values. Skip a value that is in the set. Remove a value when the heap evicts it.
A heap counts positions, not values. Two equal amounts fill two places. A set counts different values. So a set beside the heap fixes it.
Try the amounts fifty, thirty, fifty, forty, with k equal to three. The second fifty is in the set, so it is skipped. Forty is the third different amount, so a threshold exists now. The threshold is thirty, the smallest of the three.
Test k larger than the number of items. Test k equal to one. Test equal amounts at the edge.
Repeats count once. Keep a set beside the heap. Full breakdown in the lecture.