Service Desk Patience
MediumOnline Assessment
Simulate a help desk in which customers wait in a single line, give up if they wait too long, and agents take regular breaks.
The desk has agents agents numbered 0 to agents - 1. customers[i] = [arrival, service, patience] arrives at time arrival, needs service ticks of an agent's time, and will wait at most patience ticks. Everyone who is waiting is in one first-in-first-out line. Time moves in whole ticks starting at 0, and at every tick t the following happens in this order:
- Arrivals. Customers whose
arrivalistjoin the back of the line, in input order if several arrive at the same tick. - Service. Go through the agents in increasing order of number. Each agent who is free at
ttakes the customer at the front of the line, if any, and starts serving them att. (An agent is free attif they are not serving anyone and not on a break, with the rule below.) The customer's start time ist. - Abandonment. Every customer still in the line whose
arrival + patienceequalstgives up and leaves the line. A customer with patience 0 therefore leaves at their arrival tick unless an agent could take them in step 2.
A customer served from t for service ticks leaves the agent at t + service (so that agent is free again at tick t + service). An agent also counts the customers they have served: when an agent starts serving their cycle-th customer since their last break (that is, cycle, 2 * cycle, ... customers in total), they go on a break as soon as they finish that customer. The break lasts rest ticks, so the agent is free again at t + service + rest. With rest = 0 there is no break. Customers who give up never count as served.
Input: agents, cycle and rest, integers, and customers, a list of [arrival, service, patience] triples.
Output: a list with one entry per customer, in input order: [agent, start] for a customer who was served, or [-1, -1] for one who gave up.
Example 1
- Input:
- [2,2,3,[[0,4,9],[0,3,9],[1,2,2],[2,2,5],[3,1,1],[7,2,0]]]
- Output:
- [[0,0],[1,0],[1,3],[0,4],[-1,-1],[-1,-1]]
- Explanation
- Both agents (0 and 1) take customers 0 and 1 at time 0. Customer 2 arrives at 1 with patience 2, so its deadline is 3; agent 1 frees at exactly 3, and service is assigned before abandonment is checked, so it is served at 3. This is agent 1's second customer, so it takes a break after finishing at 5 and is away until 8. Customer 4 (arrival 3, patience 1) has a deadline of 4; at 4 agent 0 frees up and takes customer 3, who is ahead in the queue, so customer 4 gives up. Agent 0 also completes its second customer at 6, so it is away until 9. Customer 5 (patience 0) arrives at 7 with both agents on break and leaves immediately.
Example 2
- Input:
- [1,2,2,[[0,3,5],[1,2,3],[2,2,3],[3,1,6],[4,1,0],[9,1,0]]]
- Output:
- [[0,0],[0,3],[-1,-1],[0,7],[-1,-1],[0,9]]
- Explanation
- With a single agent: customer 0 runs 0-3. Customer 1 (arrived 1, patience 3, deadline 4) starts at 3. That is the agent's second customer, so after finishing at 5 there is a 2-tick break until 7. Customer 2 (arrived 2, patience 3) hits its deadline at 5 and leaves. Customer 3 starts at 7. Customer 4 (arrival 4, patience 0) finds the agent busy and leaves at once. At 8 the agent has finished customer 3 (its first after the break), so customer 5 at 9 is served immediately.
Example 3
- Input:
- [3,1,0,[[0,5,9],[0,1,9],[1,1,9],[1,3,9],[2,1,0],[2,2,1],[2,1,5],[3,1,0]]]
- Output:
- [[0,0],[1,0],[1,1],[2,1],[1,2],[1,3],[2,4],[-1,-1]]
- Explanation
- Three agents, no breaks. At time 0 customer 0 goes to agent 0 and customer 1 to agent 1. At time 1 agents 1 and 2 are free (agent 1 just finished): customer 2 gets the lowest-numbered free agent, 1, and customer 3 gets agent 2. At time 2, customer 4 (patience 0) is served at once by agent 1; customer 5 (deadline 3) and customer 6 wait. At 3, agent 1 frees: customer 5 is served at its deadline. Customer 7 (patience 0) finds no free agent and leaves. At 4 agent 2 frees and customer 6 starts.
Constraints
- 1 ≤ agents ≤ 6, 1 ≤ cycle ≤ 6, 0 ≤ rest ≤ 20
- 0 ≤ customers.length ≤ 100; each customer is [arrival, service, patience] with 0 ≤ arrival ≤ 1000, 1 ≤ service ≤ 30 and 0 ≤ patience ≤ 50
- Customers are not necessarily sorted by arrival