We have a queue of people, each wanting to buy a certain number of tickets. The process works like a round-robin: each person at the front buys one ticket (taking 1 second), then goes to the back of the line if they still need more. Once someone has bought all their tickets, they leave the queue entirely.
We need to find how many seconds pass before the person at index k finishes buying all their tickets.
We do not have to simulate the entire queue. The time each person contributes depends only on their position relative to person k and how their ticket count compares to person k's, which leads to a single-pass formula.
1 <= n <= 100 and 1 <= tickets[i] <= 100 → The total number of buy operations is at most sum(tickets), which is bounded by 100 * 100 = 10,000. A direct second-by-second simulation runs comfortably within these bounds.0 <= k < n → k is always a valid index, so there is no empty-input or out-of-range case to guard against.Do exactly what the problem describes. Maintain a queue, let each person at the front buy one ticket, decrement their count, and send them to the back if they still need more. Count each second as it passes. When person k's ticket count reaches zero, return the total time.
This models the round-robin process step by step. The code mirrors the problem statement directly, so its correctness is easy to confirm.
tickets. In the worst case, the queue runs for sum(tickets) total iterations.The simulation steps through every second, but each person's total contribution can be computed directly from the array. The next approach replaces the simulation with a single pass.
Rather than simulating the queue round by round, compute how much time each person contributes before person k finishes.
Person k needs tickets[k] tickets, so the queue cycles through at most tickets[k] rounds (some people drop out earlier). Each person's contribution depends on their position relative to k:
min(tickets[i], tickets[k]) seconds each. If they need fewer tickets than person k, they buy all of theirs and leave; if they need more, they buy exactly tickets[k] before person k finishes.min(tickets[i], tickets[k] - 1) seconds each.Summing these contributions in one pass gives the answer.
Relative order in the queue never changes: each round processes the survivors left to right, so a person at a smaller index always buys before a person at a larger index. Person k stops the moment their count hits zero, which is during their tickets[k]-th turn, at position k in that round. Everyone before k in that final round has already bought; everyone after k has not, which is the source of the tickets[k] versus tickets[k] - 1 split.
The min accounts for people who exhaust their own tickets first. A person can buy at most tickets[i] times no matter how many rounds run, so capping each contribution at tickets[i] correctly drops them out of later rounds.
time = 0.i from 0 to n-1:i <= k, add min(tickets[i], tickets[k]) to time.i > k, add min(tickets[i], tickets[k] - 1) to time.time regardless of input size. No extra data structures needed.