Optimize Package Order by Sorting One Window
Problem statement
Given an integer array of package priorities and a window size k, choose exactly one contiguous window of length k and sort only that window in ascending order.
Return the lexicographically smallest complete array obtainable by that operation.
Function
optimizePackageOrder(packages: int[], k: int) → int[]Examples
Example 1
packages = [3,2,1,4]k = 3return = [1,2,3,4]Sorting the first three values produces the smallest possible first element.
Example 2
packages = [1,4,3,2]k = 2return = [1,3,4,2]The window [4,3] is the first choice that improves the array lexicographically.
Constraints
1 <= packages.length <= 2001 <= k <= packages.length- Package priorities fit in signed 32-bit integers.