VIP and Regular Service Scheduler
Problem statement
A walk-in service center gives each customer who checks in a ticket with the next service number, starting at 1. Ticket numbers are shared by both kinds of customers. Staff then call customers one at a time, in the order a scheduler decides. Customers are either VIP or regular, and within each kind they are always called in check-in order.
Process events in order. "VIP" and "REGULAR" check in one customer of that kind. "NEXT" calls the next customer and records their ticket number, or -1 if nobody is waiting. The scheduler follows policy:
"VIP_FIRST": call a waiting VIP whenever there is one. Call a regular only when no VIP is waiting."TWO_TO_ONE": serve VIPs and regulars at a 2:1 rate. Count the VIPs called since the last regular was called. If a VIP is waiting and that count is below 2, call the VIP. If the count has reached 2 and a regular is waiting, call the regular, and the count starts over at 0. If only one kind is waiting, call that kind; calling a regular always resets the count, and calling a VIP always adds one to it. A"NEXT"that finds nobody waiting changes nothing.
Return the recorded results of all "NEXT" events, in order.
Function
serveCustomers(policy: String, events: String[]) → int[]Examples
Example 1
policy = "VIP_FIRST"events = ["REGULAR","VIP","REGULAR","VIP","NEXT","NEXT","NEXT","NEXT","NEXT"]return = [2,4,1,3,-1]Tickets 1 to 4 go to a regular, a VIP, a regular and a VIP. Under VIP_FIRST both VIPs are called first, in arrival order (tickets 2 and 4), then the regulars (tickets 1 and 3). The fifth call finds nobody waiting and returns -1.
Example 2
policy = "TWO_TO_ONE"events = ["VIP","VIP","VIP","REGULAR","REGULAR","VIP","NEXT","NEXT","NEXT","NEXT","NEXT","NEXT"]return = [1,2,4,3,6,5]VIPs hold tickets 1, 2, 3 and 6 and regulars hold 4 and 5. The calls go VIP, VIP, regular, VIP, VIP, regular: tickets 1, 2, 4, 3, 6, 5. After two VIPs in a row, a waiting regular is always next.
Example 3
policy = "TWO_TO_ONE"events = ["REGULAR","REGULAR","NEXT","VIP","NEXT","NEXT","NEXT"]return = [1,3,2,-1]No VIP is waiting at the first call, so regular ticket 1 is called and the VIP streak stays at 0. VIP ticket 3, who checked in after that call, is called next. Then regular ticket 2 is the only one waiting. The last call finds nobody and returns -1.
Constraints
policyis"VIP_FIRST"or"TWO_TO_ONE"1 <= events.length <= 2000- Each event is
"VIP","REGULAR"or"NEXT", and at least one event is"NEXT".