All problems
0667EasyArrayTwo PointersSimulation

Settle the Conveyor Trays

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2460Apply Operations to an Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def settle_line(counts: list[int]) -> list[int]:
Java
public int[] settleLine(int[] counts)
September 7
Apply