Distribute Elements Into Two Arrays I
Real-world version: Splitting Parcels Onto Two Intake TraysThe statement here is our own: the same inputs, outputs and technique.
Parcels come off a chute one after another, their weights listed in weights. No two parcels weigh the same. A sorter stacks them on two trays, each new parcel going on top of the tray it is sent to.
The first parcel goes on the near tray and the second on the far tray. Every parcel after that is placed by looking at the parcel currently on top of each tray: if the near tray's top parcel is heavier than the far tray's top parcel, the new parcel goes on the near tray, and otherwise it goes on the far tray.
Return one list holding the near tray read from the bottom up, followed by the far tray read from the bottom up.
Examples
Example 1
- Input
- weights = [7, 4, 9, 2, 6]
- Output
- [7, 9, 2, 4, 6]
Parcel 7 goes near and parcel 4 goes far. Parcel 9 arrives with tops 7 and 4, so it goes near; parcel 2 arrives with tops 9 and 4, so it goes near; parcel 6 arrives with tops 2 and 4, so it goes far. The near tray reads 7, 9, 2 and the far tray reads 4, 6.
Example 2
- Input
- weights = [9, 8, 7, 6]
- Output
- [9, 7, 8, 6]
Parcel 7 arrives with tops 9 and 8 and goes near. Parcel 6 then arrives with tops 7 and 8 and goes far, leaving the near tray as 9, 7 and the far tray as 8, 6.
Example 3
- Input
- weights = [4, 100, 3, 99, 2]
- Output
- [4, 99, 2, 100, 3]
Parcel 3 arrives with tops 4 and 100 and goes far. Parcel 99 then arrives with tops 4 and 3 and goes near, and parcel 2 arrives with tops 99 and 3 and goes near.
Constraints
- n == weights.length
- 3 <= n <= 50
- 1 <= weights[i] <= 100
- The weights are all different.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def split_parcels(weights: list[int]) -> list[int]:public int[] splitParcels(int[] weights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's