Shopping and Billing
Updated
Problem statement
A shop has N billing counters. M people come to pay, and person i arrives at time time[i]. Each person goes to the counter with the shortest queue, meaning the counter with the fewest people already there (waiting or being billed). If that counter is empty, the person is billed right away. Otherwise the person joins the end of its queue. For every person, return the time when billing finishes and the person leaves the counter.
For example, with N = 2 and four people who all arrive at time 0, the first two go to different empty counters and finish at time 1. The other two each join a queue of one person and finish at time 2. The answer is [1, 1, 2, 2].
Rules:
- Billing one person takes 1 unit of time.
- A counter starts the next person in its queue as soon as the current person leaves.
- A person who finishes at time
tis gone at timet, so someone arriving at timetdoes not count that person. - If several counters have the same shortest queue, the person takes the one with the smallest number. This does not change the answer.
- People are listed in the order they arrive, so
timenever goes down. People with the same time are handled in array order.
Complete the function billingTime in the editor.
billingTime has the following parameters:
int N: the number of countersint M: the number of peopleint[] time: the arrival time of each person
The function returns an int[] of length M: the time each person finishes billing.
Function
billingTime(N: int, M: int, time: int[]) → int[]Examples
Example 1
N = 2M = 4time = [0, 0, 0, 0]return = [1, 1, 2, 2]All four people arrive at the same time (time 0). The first two people will go to the two available counters and finish billing at time 1. The next two people will have to wait until the first two leave, so they start billing at time 1 and finish at time 2.
Constraints
1 ≤ N ≤ 1051 ≤ M ≤ 105, whereMis the length oftime0 ≤ time[i] ≤ 109time[i] ≤ time[i + 1]: people are listed in arrival order
More WeWork interview questions
Similar questions at other companies
- Split Tender Part 2 - Expiration DatesStripe · OAStripe interview questions
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions
- Find Total CostJP Morgan Chase · OAJP Morgan Chase interview questions
- Expiring Authentication Token ManagerIBM · OAIBM interview questions
- Inventory AllocationAmazon · OA
- Women in STEM CharityCanva · OACanva interview questions
- Load BalancerTikTok · OATikTok interview questions
- Worst Trade ReporterOptiver · OAOptiver interview questions
- Progressive Banking SystemAirbnb · OAAirbnb interview questions
- Named Async Task SchedulerAirwallex · OAAirwallex interview questions