Efficient Package Priority Sorting
At Delhivery, efficient package sorting is crucial for optimizing loading and delivery routes. To streamline operations, packages are categorized by handling priority. For certain truck loads, all 'Priority' packages must be grouped together at the beginning of the sequence, followed by 'Standard' packages. This ensures that high-priority items are handled first during loading and unloading, reducing potential delays and improving overall logistics efficiency.
Implement a function sortPackagesByPriority that takes an array of strings, packagePriorities, representing the handling priority of various packages. The function should rearrange the array in-place such that all packages with the "Priority" type appear before all packages with the "Standard" type. The relative order within the "Priority" group or the "Standard" group does not matter.
Function Signature:
function sortPackagesByPriority(packagePriorities: string[]): void
Input:
packagePriorities: An array of strings. Each string will be either "Priority" or "Standard".Output:
packagePriorities array in-place. It does not need to return anything explicitly (void).packagePriorities is empty, the function should do nothing.Examples
Example 1
Input:
packagePriorities = ["Standard", "Priority", "Standard", "Priority", "Standard"]
Output:
["Priority", "Priority", "Standard", "Standard", "Standard"]
Note:
All "Priority" packages are moved to the beginning of the array, and "Standard" packages to the end. The exact order within the "Priority" or "Standard" groups does not matter, as long as all "Priority" come before all "Standard".
Example 2
Input:
packagePriorities = ["Priority", "Priority", "Priority"]
Output:
["Priority", "Priority", "Priority"]
Note:
All packages are already "Priority", so the array remains unchanged as it already satisfies the condition.
Example 3
Input:
packagePriorities = ["Standard", "Standard", "Priority"]
Output:
["Priority", "Standard", "Standard"]
Note:
The single "Priority" package is moved to the front of the array to satisfy the grouping requirement.
Example 4
Input:
packagePriorities = []
Output:
[]
Note:
An empty input array results in an empty array, as there are no packages to sort.
Constraints
packagePriorities array will be between 0 and 10^5.packagePriorities will be one of two strings: "Priority" or "Standard".O(N), where N is the length of packagePriorities.O(1) (constant extra space).O(N) time and O(1) space complexity requirements.Loading editor...