Container Yard Crane
MediumOnline Assessment
Drive the crane of a container yard that must dig out requested boxes from stacks and parks the boxes it moves out of the way by fixed rules.
The yard has several stacks of containers, each listed from the bottom box to the top box. Every container has a distinct integer id, and no stack may ever be taller than height boxes. A smaller id leaves the yard earlier, so a box is "in the way" of anything beneath it that has a smaller id.
The crane processes targets in order. To retrieve a target:
- If the target is not in the yard, skip it (it costs nothing).
- Otherwise every box above it must be relocated, one at a time, starting with the top box of that stack. Each relocation moves the box onto some *other* stack that currently has fewer than
heightboxes (the box above the target's stack is never put back on it). The destination is chosen by these rules, in this order:
- Among non-empty candidate stacks whose top id is greater than the moving box's id, the one whose top id is smallest.
- Otherwise, the empty candidate stack with the lowest index.
- Otherwise, the candidate stack with the largest top id.
- If there is no candidate stack at all (every other stack is full), the retrieval is blocked: stop relocating at once, leave the target in the yard, and go on to the next target. Relocations already made stay made.
- When nothing is left above the target, the crane lifts it out of the yard. Lifting the target is not a relocation.
Input: height, the maximum stack height; stacks, the initial stacks as lists of ids from bottom to top (some may be empty, none is taller than height); targets, the ids to retrieve in order.
Output: a list whose first element is a one-element list with the total number of relocations, followed by the final stacks in the same order, each from bottom to top.
Example 1
- Input:
- [4,[[9,3,6],[5,8],[7],[]],[3,5]]
- Output:
- [[2],[9,8],[],[7,6],[]]
- Explanation
- Digging out 3 means moving 6 first. Stacks 1 and 2 have greater tops (8 and 7); the smallest is 7, so 6 goes onto stack 2. Then 3 is lifted. For target 5, the box 8 goes onto stack 0, whose top 9 is the only greater one. Two relocations in total.
Example 2
- Input:
- [3,[[1,4,3],[6,5,7],[8,9]],[1,4,7,1]]
- Output:
- [[1],[],[6,5],[8,9,3]]
- Explanation
- Target 1 sits under 4 and 3. The 3 has two stacks with greater tops, but stack 1 is full (3 boxes), so it goes onto stack 2 (top 9). Now no other stack has room for the 4: the retrieval is blocked and 1 stays. Target 4 is now the top of stack 0 and is lifted; 7 is lifted from stack 1; the last 1 is lifted. One relocation.
Example 3
- Input:
- [3,[[6,2,9],[4],[7,3,1],[]],[6,2,77]]
- Output:
- [[2],[],[4],[7,3,1],[9]]
- Explanation
- Target 6 has 2 and 9 above it. Box 9 has no greater top anywhere (stack 2 is full anyway), so rule 2 sends it to the empty stack 3. Box 2 goes onto the smaller of the greater tops, 4 (stack 1), not 9. After 6 is lifted, 2 is lifted from stack 1; target 77 is not in the yard. Two relocations.
Constraints
- 1 ≤ height ≤ 12; 1 ≤ stacks.length ≤ 10; stacks are not taller than height
- Container ids are distinct integers in 1..106
- 0 ≤ targets.length ≤ 60; targets may name ids that are not in the yard, or repeat