← 返回 salesforce 的题目列表Order a Nearly Sorted UDP Packet Stream
类型:online_judge
Order a Nearly Sorted UDP Packet Stream
You continuously receive a stream of integer UDP packet sequence numbers. Every number is at most k positions away from its position in the fully sorted order. Given buffer size k, output all numbers in ascending order without storing the entire stream.
Implement sort_nearly_sorted(stream, k).
Example
Input: stream = [6, 5, 3, 2, 8, 10, 9], k = 3
Output: [2, 3, 5, 6, 8, 9, 10]
Constraints
0 <= k <= 10^6
The stream can be much larger than available memory.
Every item is at most k positions from its final sorted position.
Extra space must be O(k).
Example
Input
7 3
6 5 3 2 8 10 9
Output
2 3 5 6 8 9 10