mediumArraysPattern: Two Pointers
Weight Category Partition Solution
Problem Statement
You are given a list of integers `weights` representing the weights of cargo containers. Partition the list into two categories: weights less than 850 and weights greater than or equal to 850, while maintaining the relative order within each category.
Examples
Example 1:
Input:[500, 1200, 300, 900, 1000]
Output:[500, 300, 1200, 900, 1000]
Explanation: Containers with weights less than 850 kg are placed first in their original order, followed by containers with weights 850 kg or more in their original order
Constraints
- 1 <= cargoWeights.length <= 10^5
- 1 <= cargoWeights[i] <= 10^6
- 1 <= targetWeight <= 10^6
Time: O(n) Space: O(n)
The optimal approach involves utilizing the Two Pointers technique to iterate through the cargoWeights array and partition it into two subarrays based on the target weight, preserving the original order. This can be achieved with a single pass through the array.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
