Sorting Diverter Line
MediumOnline Assessment
Send parcels down a belt past a row of diverter gates and report where each one ends up.
gates[i] = [dest, capacity] is the (i+1)-th gate, in belt order. Each gate has a chute that starts empty and holds at most capacity parcels. A gate's dest is a destination code, or ?, which matches every destination.
items is the stream on the belt, one item per slot, numbered from 1. An item is either a parcel (its destination code) or "*", a truck. A truck empties every chute at once and sends nothing down the belt, but it still uses up its slot.
A parcel in slot t passes the gates in order and is diverted by the first gate that is ready for it. A gate is ready when all of these hold:
- its
destequals the parcel's destination, or is?; - its chute is not full;
- it is not resting: after a gate diverts a parcel in slot
sit rests through the nextcooldownslots, so it can divert again from slots + cooldown + 1on. (Passing a parcel by does not make a gate rest; withcooldown = 0a gate never rests.)
If no gate is ready the parcel reaches the end of the belt and goes into the overflow bin.
Input: gates, cooldown, items.
Output: [results, loads]. results has one number per parcel, in belt order (trucks are not listed): the 1-based number of the gate that diverted it, or 0 for the overflow bin. loads has the number of parcels in each gate's chute after the last item.
Example 1
- Input:
- [[["a",1],["b",2],["?",5]],0,["a","a","b","b","b","z"]]
- Output:
- [[1,3,2,2,3,3],[1,2,3]]
- Explanation
- Gate 1 takes
a(capacity 1), gate 2 takesb(capacity 2), gate 3 takes anything (capacity 5); cooldown 0. The firstagoes to gate 1. The secondafinds that chute full, so it falls through to the catch-all gate 3. The first twobs fill gate 2, and the thirdbfinds gate 2 full and also drops into gate 3. The unknown codezmatches no specific gate and is taken by gate 3, which now holds 3 parcels.
Example 2
- Input:
- [[["x",2],["x",1]],2,["x","x","x","x","*","x","x"]]
- Output:
- [[1,2,0,1,2,1],[1,1]]
- Explanation
- Two gates take
x; cooldown 2, so a gate that diverts in slot s is ready again in slot s + 3. Slot 1 goes to gate 1 (resting through slot 3). Slot 2: gate 1 rests, so gate 2 takes it (its chute, capacity 1, is now full). Slot 3: gate 1 still rests and gate 2 is full, so the parcel goes to the overflow bin (0). Slot 4: gate 1 is ready again. Slot 5 is a truck: it empties the chutes but does not end any rest and it uses up its slot. Slot 6: gate 1 rests until slot 7, so gate 2 (empty again and ready) takes it. Slot 7: gate 1 is ready. Final chute contents: 1 and 1.
Example 3
- Input:
- [[["?",1],["k",2]],1,["k","k","*","k","k","*","k","q","k"]]
- Output:
- [[1,2,1,2,1,0,2],[1,1]]
- Explanation
- Gate 1 accepts every destination (capacity 1), gate 2 takes
k(capacity 2); cooldown 1. Slot 1: gate 1 takesk. Slot 2: gate 1 rests, so gate 2 takes it. Slot 3 is a truck and empties both chutes. Slot 4: gate 1 is ready and takes it; slot 5: gate 1 rests so gate 2 takes it. Slot 6 is another truck. Slot 7: gate 1 takesk(it is ready from slot 6 on). Slot 8:qmatches only the catch-all gate 1, which is resting and full, so it goes to the overflow bin. Slot 9: gate 1 is ready but its chute is full, so gate 2 takes it. Results are 1, 2, 1, 2, 1, 0, 2.
Constraints
- 1 ≤ gates.length ≤ 10; a gate destination is a lowercase code of 1 to 3 letters, or
?; 1 ≤ capacity ≤ 20 - 0 ≤ cooldown ≤ 5
- 0 ≤ items.length ≤ 300; an item is a destination code of 1 to 3 lowercase letters, or
*for a truck