A heap of k plus one
transcript
A rides app gets trip times. Delays shuffle them a little. No time is far from home. Use that.
Each value is at most k places from home. So the next output is among the first k plus one values. Keep those values in a heap.
The first three values: two, three, one. The smallest is one. Write it out. Add five. Write out two. Keep going. Then empty the heap.
A full sort takes about one point six six million comparisons. A heap of eleven values takes about three hundred forty-six thousand. The heap never holds more than eleven values.
Test the edge cases before you submit. When k is zero, the list is already sorted. When k is larger than the list, the heap holds everything. And an empty list returns an empty list.
A window of k writes one value too early. With the values two, three, one, it writes two first. Wrong. A window of k plus one gives one, two, three.
Use the order that is there. Keep k plus one values. Full breakdown in the lecture.