TV Remote Tuner
MediumOnline Assessment
Track the channel a TV is tuned to while someone uses its remote control.
The TV receives the channels listed in channels (increasing order). It starts on channels[0] and has no previous channel yet. The remote has the digit buttons "0"-"9", "UP", "DOWN" and "LAST". presses is a list of [time, button] pairs in non-decreasing time order.
Digit entry. Digits typed one after another build an *entry*, up to 3 digits. An entry is committed when one of these happens, whichever comes first:
- it reaches 3 digits (immediately);
- another button is pressed at a time at least
waitafter the entry's last digit was pressed. The entry is committed first, and then that button is handled as usual (a digit starts a new entry, so two digitswaitor more apart never join); - a non-digit button is pressed (the entry is committed first, then the button works);
- the presses run out (the entry is committed).
Committing an entry reads it as a number ("007" is 7). If a channel with that number exists, the TV tunes to it. Otherwise the entry is thrown away and nothing changes. Tuning to the channel you are already on changes nothing, including the previous channel.
Other buttons. UP tunes to the next higher channel in the list and DOWN to the next lower one; both wrap around between the highest and the lowest. LAST swaps the current channel with the previous one, and does nothing if there is no previous channel. Whenever the TV changes channel, the channel it left becomes the previous channel.
Input: channels, wait, presses.
Output: the list of channels the TV was tuned to, in order, starting with the initial channel and adding a channel each time the TV actually changes to a different one.
Example 1
- Input:
- [[2,5,12,30],3,[[0,"1"],[1,"2"],[2,"UP"],[3,"LAST"]]]
- Output:
- [2,12,30,12]
- Explanation
- Channels 2, 5, 12, 30; wait 3. The TV starts on 2.
1at time 0 and2at time 1 build the entry 12, which is not yet committed.UPat time 2 commits it first, so the TV tunes to 12, and then UP moves to 30.LASTgoes back to the previous channel, 12. Route: 2, 12, 30, 12.
Example 2
- Input:
- [[1,4,10,11],2,[[0,"1"],[2,"0"],[5,"DOWN"],[6,"1"],[6,"1"],[6,"1"]]]
- Output:
- [1,11]
- Explanation
- Channels 1, 4, 10, 11; wait 2; the TV starts on 1. The digit
1at time 0 times out when0arrives at time 2 (2 ticks later is enough), so it is committed: channel 1 is where the TV already is and nothing changes. The new entry0is committed whenDOWNarrives at time 5 and is thrown away (no channel 0).DOWNfrom the lowest channel wraps to the highest, 11. The three1presses make 111 at once, which does not exist, so the TV stays on 11. Route: 1, 11.
Example 3
- Input:
- [[3,8,21],5,[[0,"LAST"],[1,"2"],[2,"1"],[3,"LAST"],[4,"0"],[5,"0"],[6,"8"],[7,"LAST"]]]
- Output:
- [3,21,3,8,3]
- Explanation
- Channels 3, 8, 21; wait 5.
LASTat the start does nothing: there is no previous channel.2then1build 21, committed byLASTat time 3, so the TV goes to 21, and then LAST returns to 3. The digits 0, 0, 8 reach three digits at time 6 and commit as 008, which is channel 8 (previous channel 3). The finalLASTgoes back to 3. Route: 3, 21, 3, 8, 3.
Constraints
- 1 ≤ channels.length ≤ 30; channel numbers are distinct integers from 1 to 999, given in increasing order
- 1 ≤ wait ≤ 100
- 0 ≤ presses.length ≤ 200; times are non-decreasing integers from 0 to 104; each button is one of
"0"-"9","UP","DOWN","LAST"