Rearrange the shipment list so all smalls come first, then mediums, then larges. Do this in a single pass.
Explanation: Step-by-step: with input [0, 1, 2, 0, 1, 2], we initialize pointers low = 0 and high = 5. We iterate through the array, swapping elements as necessary. After the first iteration, the array becomes [0, 0, 2, 1, 1, 2]. We continue iterating, swapping elements until the array becomes [0, 0, 0, 1, 1, 2, 2].
Explanation: Step-by-step: with input [2, 0, 1, 2, 0, 1], we initialize pointers low = 0 and high = 5. We iterate through the array, swapping elements as necessary. After the first iteration, the array becomes [0, 2, 1, 2, 0, 1]. We continue iterating, swapping elements until the array becomes [0, 0, 1, 1, 2, 2].
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak