Crosswalk Controller
MediumOnline Assessment
Work out what a pedestrian crossing's signal shows at given moments, given when the walk button was pressed.
The crossing lights a single phase at every whole minute t >= 0. The phases, in cycle order, are GREEN (cars may go), YELLOW, WALK (pedestrians may cross) and FLASH (pedestrians must finish crossing), and then back to GREEN. timings = [g, y, w, f] gives the lengths in minutes: YELLOW lasts exactly y minutes, WALK exactly w, FLASH exactly f. GREEN always lasts at least g minutes, and at time 0 it has just begun.
A phase that begins at minute s and lasts d minutes is shown at minutes s .. s + d - 1; the next phase starts at s + d.
The button works like this. A press at minute p is looked at after any phase change that happens at minute p:
- During
GREENit registers a request, unless a request is already waiting (then it does nothing). - During
YELLOWorWALKit does nothing: the walk is already on its way or under way. - During
FLASHit registers a request for the next green period; the pedestrian has just missed the walk.
When GREEN has a request waiting, it ends as soon as both conditions hold: it has lasted at least g minutes, and the request exists. If the request was registered at minute p and the current GREEN began at minute b, the next YELLOW therefore starts at minute max(p, b + g). A request that was registered during FLASH is already waiting when the next GREEN begins, so that GREEN lasts exactly g minutes. With no request, GREEN lasts forever.
Input: timings, as above; presses, the minutes at which the button was pressed, in non-decreasing order (duplicates are possible); queries, minutes to report, in any order.
Output: a list with the phase name ("GREEN", "YELLOW", "WALK" or "FLASH") shown at each query minute, in query order.
Example 1
- Input:
- [[5,2,4,3],[1,3,20],[0,4,5,6,7,10,11,13,14,19,20,22]]
- Output:
- ["GREEN","GREEN","YELLOW","YELLOW","WALK","WALK","FLASH","FLASH","GREEN","GREEN","YELLOW","WALK"]
- Explanation
- With timings [5, 2, 4, 3], the press at minute 1 registers a request, but GREEN must last 5 minutes, so YELLOW starts at minute 5 (the press at minute 3 changes nothing). YELLOW is shown at minutes 5-6, WALK at 7-10, FLASH at 11-13, and GREEN returns at minute 14. The press at minute 20 registers during GREEN, which already lasted more than 5 minutes, so YELLOW starts immediately at minute 20 and WALK begins at 22.
Example 2
- Input:
- [[4,1,3,2],[10,11,13,14,15,21],[9,10,11,13,14,15,16,19,20,21,24,26]]
- Output:
- ["GREEN","YELLOW","WALK","WALK","FLASH","FLASH","GREEN","GREEN","YELLOW","WALK","FLASH","GREEN"]
- Explanation
- With timings [4, 1, 3, 2], the press at minute 10 starts YELLOW at once (WALK at 11-13, FLASH at 14-15, GREEN at 16). Presses at 11 and 13 fall in WALK and are ignored. The press at 14 falls in FLASH, so it registers a request for the next green period (the one at 15 changes nothing more). That GREEN starts at minute 16 with a request already waiting, so it lasts exactly 4 minutes and YELLOW starts at 20, WALK at 21. The press at 21 is during WALK and is ignored.
Example 3
- Input:
- [[3,2,2,2],[0,9,9,15],[2,3,8,9,11,12,15,16,17,18]]
- Output:
- ["GREEN","YELLOW","FLASH","GREEN","GREEN","YELLOW","WALK","FLASH","FLASH","GREEN"]
- Explanation
- With timings [3, 2, 2, 2], the press at minute 0 waits for the 3-minute minimum, so YELLOW starts at 3, WALK at 5, FLASH at 7 and GREEN at 9. Two presses arrive exactly at minute 9: the change to GREEN happens first, so they register during GREEN, and the minimum green counts from minute 9, giving YELLOW at 12. The press at 15 is during WALK (14-15) and ignored; FLASH is 16-17 and GREEN is back at 18.
Constraints
- timings = [g, y, w, f] with 1 ≤ g ≤ 20 and 1 ≤ y, w, f ≤ 12
- 0 ≤ presses.length ≤ 80; the presses are non-decreasing and each is between 0 and 600
- 1 ≤ queries.length ≤ 80; each query is between 0 and 700