Last Updated: March 29, 2026
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 figure out exactly how many seconds pass before the person at index k finishes buying all their tickets.
The key insight here is that we do not actually need to simulate the entire queue. We can figure out how much time each person contributes to the total wait by thinking about their position relative to person k and how many tickets they need compared to person k.
1 <= n <= 100 → The array is tiny. Even an O(n^2) simulation with nested loops runs in at most 10,000 operations, which is instant.1 <= tickets[i] <= 100 → Each person wants at most 100 tickets. Combined with n <= 100, the total simulation steps are at most 10,000.0 <= k < n → k is always a valid index. Given these small constraints, even a direct simulation is perfectly fine, but there is a clean O(n) mathematical approach worth learning.The most straightforward way to solve this is to 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 hits zero, return the total time.
This is essentially modeling the round-robin process step by step. It is easy to reason about correctness because the code mirrors the problem statement directly.
tickets. In the worst case, the queue runs for sum(tickets) total iterations.This approach works correctly but simulates every single second of the process. Most of the simulation is predictable. Can we calculate each person's contribution directly, without simulating the queue at all?
Instead of simulating the queue round by round, let's think mathematically about how much time each person contributes before person k finishes.
Person k needs tickets[k] tickets. So the queue will cycle through at most tickets[k] full rounds (though some people drop out earlier). Here is the key observation:
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] tickets before person k finishes.min(tickets[i], tickets[k] - 1) seconds each.That is the entire solution. One pass through the array, summing up each person's contribution.
Think of the queue as cycling through rounds. In each complete round, every remaining person buys one ticket, left to right. Person k finishes at the end of their tickets[k]-th round. But crucially, they finish partway through that final round, right at position k.
So anyone at or before position k in the original array gets to buy a ticket in the final round (because they come before k in the ordering). Anyone after position k does not get to buy in the final round. They only participate in the first tickets[k] - 1 complete rounds.
Of course, some people leave the queue early because they run out of tickets to buy. That is why we take the minimum: a person can contribute at most their own ticket count, regardless of how many rounds would theoretically pass.
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.