FastPrepShopping and Billing

Shopping and Billing

WeWork logoWeWork● MediumFULLTIMEOA
Learn

Problem statement

In a shop with N counters, M people arrive for billing at different times denoted as time[i]. Each person selects the counter with the shortest queue, based on the number of people already present. If a counter is empty, the person gets immediate billing, otherwise, they join the queue.

For every person, output the time when they finish billing and leave the counter.

Notes

  • It takes 1 unit of time for the counter to process a person's bill.
  • The counter processes the next person immediately after the current person leaves.

Input/Output

  • The first line contains N denoting the number of counters.
  • The second line contains M denoting the number of persons.
  • The third line contains an array time, indicating the entry time of the people.

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.

More WeWork problems

See WeWork hiring insights
public int[] billingTime(int N, int M, int[] time) {
  // write your code here
}
N2
M4
time[0, 0, 0, 0]
expected[1, 1, 2, 2]
Checking account…