Your question is Optimal Sorting with Heaps. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Asana may provide a sequence of tasks that is almost sorted by due date. Every task is at most k positions away from its position in the fully sorted sequence. Given an integer array, return its sorted order using an algorithm that exploits this guarantee.
Implement sort_nearly_sorted(nums, k). The function must return a new array containing the values from nums in nondecreasing order. Do not call Python's built-in sorting functions.
nums, a list of integers, and k, a nonnegative integer.nums in nondecreasing order.k-sorted: each value is at most k indices from its final sorted index.def sort_nearly_sorted(nums, k):