Minimum Number Game
Real-world version: Shelving Boxes in TurnsThe statement here is our own: the same inputs, outputs and technique.
Two archivists, Ada and Ben, clear a pile of boxes. Each box carries a label, given as the list labels, and the pile holds an even number of boxes.
They repeat the following round until the pile is empty:
- Ada lifts the box with the smallest label still in the pile.
- Ben lifts the box with the smallest label still in the pile.
- Ben places his box at the end of the shelf.
- Ada places her box at the end of the shelf.
Labels may repeat; when two boxes carry the same label it makes no difference which of them is lifted, because the shelf reads the same either way.
Return the list of labels along the shelf, from the first box placed to the last.
Examples
Example 1
- Input
- labels = [12, 9, 4, 30]
- Output
- [9, 4, 30, 12]
Round one: Ada lifts `4`, Ben lifts `9`, so the shelf starts `9`, `4`. Round two: Ada lifts `12`, Ben lifts `30`, so the shelf continues `30`, `12`.
Example 2
- Input
- labels = [8, 6, 6, 8, 6, 8]
- Output
- [6, 6, 8, 6, 8, 8]
The first two rounds each lift a `6` and a `6`, and a `6` and an `8`; the last round lifts the remaining two `8`s. Placing each round's second box first gives the shelf shown.
Example 3
- Input
- labels = [100, 1, 2, 99]
- Output
- [2, 1, 100, 99]
Ada lifts `1` and Ben lifts `2` in the first round, then Ada lifts `99` and Ben lifts `100`. Ben places before Ada each round.
Constraints
- 2 <= labels.length <= 100
- 1 <= labels[i] <= 100
- labels.length is even.
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 shelve_boxes(labels: list[int]) -> list[int]:public int[] shelveBoxes(int[] labels)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Car PoolingReal-world version: One-Way Tram Capacity CheckMedium
- Meeting Rooms IIIReal-world version: Busiest Dye VatHard
- Maximum Product of Two Elements in an ArrayReal-world version: Best Pair Of Voucher PayoutsEasy