1125. Bakery Line Rejoin

Easy1.5 points estimatedQueueSimulationArray

A bakery sells loaves to one customer at a time. People stand in a line, and person i (0-based, in the initial line order) wants needs[i] loaves. Each minute the person at the front buys exactly one loaf. If they still want more loaves they walk to the back of the line, otherwise they leave happy.

Simulate the line and return the indices of the people in the order they finish buying. No two people finish in the same minute, so the order is unique.

Constraints: 1 <= n <= 100 and 1 <= needs[i] <= 100. A queue holding indices gives at most sum(needs) steps, which is O(n * max(needs)) time, and O(n) extra space.

Example 1

Input:
needs = [2,1,3]
Output:
[1,0,2]
Explanation:

Minute 1: person 0 buys and rejoins, minute 2: person 1 finishes, minute 3: person 2 buys, minute 4: person 0 finishes, then person 2 finishes.

Example 2

Input:
needs = [1,1,1]
Output:
[0,1,2]
Explanation:

Everybody needs one loaf, so they finish in line order 0, 1, 2.

Constraints

1 ≤ n ≤ 100

1 ≤ needs[i] ≤ 100

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(sum of needs)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…