Loading...
Loading...
Loading...
A log is almost in time order: every record is at most k places away from where it belongs in fully sorted order. Print the records in sorted order, using a heap of size k + 1 rather than sorting the whole list.
Example 1
Input
2 3 2 1 5 4
Output
1 2 3 4 5
Example 2
Input
0 1 2 3
Output
1 2 3
Try the problem first. Open one hint at a time.
The smallest unplaced value must be among the next k + 1 values.
Push values into a min-heap, and once it holds k + 1 values, pop the smallest each time.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor