Turnstile tally
EasyOnline Assessment
Tally what a station turnstile did during a shift, given the log of events it saw.
The turnstile is always in one of three states and starts locked. Each character of events is one event:
C(a coin is inserted): if the turnstile is locked, it keeps the coin and becomes open. In any other state the coin is returned to the customer.P(someone pushes): if the turnstile is open, one person passes through and it becomes locked again. In any other state nothing happens.M(staff start maintenance): from any state the turnstile goes into maintenance. A coin already kept for a passage is not refunded.R(staff resume): only a turnstile in maintenance reacts, and it returns to locked. In any other state nothing happens.
Input: events, a string of C, P, M and R characters (possibly empty).
Output: a list of three integers [passed, kept, returned]: how many people passed, how many coins were kept, and how many coins were returned.
Example 1
- Input:
- ["CPCCP"]
- Output:
- [2,2,1]
- Explanation
Ckept 1 coin (open),Ppasses one person (locked),Ckept a second coin (open), the nextCarrives while open and is returned, and the finalPpasses a second person:[2, 2, 1].
Example 2
- Input:
- ["CMCRP"]
- Output:
- [0,1,1]
- Explanation
Ckeeps a coin (open).Mputs the turnstile in maintenance, and the kept coin is not refunded. The nextCis returned,Rlocks it again, and the finalPfinds it locked:[0, 1, 1].
Example 3
- Input:
- ["CRPPC"]
- Output:
- [1,2,0]
- Explanation
Ckeeps a coin (open). TheRdoes nothing because the turnstile is not in maintenance, so the firstPstill passes one person. The secondPfinds it locked. The lastCis kept:[1, 2, 0].
Constraints
0 <= events.length <= 5,000eventscontains only the charactersC,P,MandR