Apply Operations to an Array
Real-world version: Settle the Conveyor TraysThe statement here is our own: the same inputs, outputs and technique.
A conveyor carries a row of trays. counts[i] is how many parts sit in tray i, and a tray holding 0 parts is empty.
A settling run walks the row once from left to right. For each index i from 0 up to counts.length - 2, in that order, it looks at trays i and i + 1 as they stand at that moment. If both hold the same number of parts and that number is not 0, the contents of tray i + 1 are tipped into tray i, so tray i then holds twice as many parts as before and tray i + 1 becomes empty. Otherwise both trays are left alone. Every index is visited exactly once.
When the walk is over, the trays that still hold parts are pushed to the front of the row in the order they already had, and the empty trays follow them.
Return the row of tray contents after the settling run and the push.
Examples
Example 1
- Input
- counts = [8, 8, 5, 5, 5]
- Output
- [16, 10, 5, 0, 0]
At index `0` the two trays of `8` become a tray of `16` and an empty tray. At index `1` the trays hold `0` and `5`, which are different, so nothing happens. At index `2` the two trays of `5` become `10` and an empty tray. At index `3` the trays hold `0` and `5`. Pushing the loaded trays forward gives `16, 10, 5` followed by the two empties.
Example 2
- Input
- counts = [6, 6, 6, 6, 6, 6]
- Output
- [12, 12, 12, 0, 0, 0]
Indices `0`, `2` and `4` each tip a pair of `6`s into a `12`, and at indices `1` and `3` the left tray is already empty. Three loaded trays are pushed to the front.
Example 3
- Input
- counts = [4, 0, 4]
- Output
- [4, 4, 0]
No index has two equal non-empty trays side by side, so no tipping happens and only the push takes place.
Constraints
- 2 <= counts.length <= 2000
- 0 <= counts[i] <= 1000
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 settle_line(counts: list[int]) -> list[int]:public int[] settleLine(int[] counts)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 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
More on the same topics
- Rearrange Array Elements by SignReal-world version: Interleaving the Statement LinesMedium
- Candy CrushReal-world version: Settling the Colour Match BoardMedium
- Partition Array According to Given PivotReal-world version: Reorder Readings Around a ReferenceMedium
- Sum of GCD of Formed PairsReal-world version: Total Mesh Cycle of the Gear BinMedium