Time Needed to Buy Tickets (LeetCode 2073): Solution and Explanation
People stand in a line, each wanting some number of tickets. A purchase takes one second, and anyone who still needs more goes to the back of the line. Return how many seconds pass before the person at index k has all of their tickets.
Intuition
The direct approach simulates the queue: pop the front person, sell one ticket, push them to the back if they still need more, and stop when person k reaches zero. With at most 100 people wanting at most 100 tickets that is fast enough, but it hides a much simpler counting argument.
The line moves in rounds. In every round each remaining person buys exactly one ticket, in their original order, because nobody overtakes anyone. Person k therefore finishes in round tickets[k], at the moment they buy their own ticket in that round.
Count how many seconds each person contributes before that moment. Someone standing at or before k gets a turn in every one of those rounds, so they buy min(tickets[i], tickets[k]) tickets. Someone behind k misses the final round, because k leaves before their turn comes, so they buy min(tickets[i], tickets[k] - 1).
Every ticket bought takes one second, so adding those capped counts gives the answer in a single pass with no queue at all.
Approach: Capped count per person
- Set
total = 0. - For each index i: if
i <= k, addmin(tickets[i], tickets[k])tototal. - Otherwise add
min(tickets[i], tickets[k] - 1), since person i never gets a turn in the round where person k finishes. - Return
total.
Step-by-step example
tickets = [2,3,2], k = 2
- Person 2 wants 2 tickets, so they finish in round 2. The cutoff for indexes 0 to 2 is 2, and nobody stands behind them.
- i = 0: wants 2, cutoff 2, adds 2.
total= 2. - i = 1: wants 3, but only 2 rounds happen before person 2 is done, adds 2.
total= 4. - i = 2: this is person k, adds all 2 of their tickets.
total= 6. - Return 6, which matches simulating two full rounds of three people.
Code: Python, Java, C++
1class Solution:
2 def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
3 total = 0
4 for i in range(len(tickets)):
5 if i <= k:
6 total += min(tickets[i], tickets[k])
7 else:
8 total += min(tickets[i], tickets[k] - 1)
9 return total1class Solution {
2 public int timeRequiredToBuy(int[] tickets, int k) {
3 int total = 0;
4 for (int i = 0; i < tickets.length; i++) {
5 if (i <= k) {
6 total += Math.min(tickets[i], tickets[k]);
7 } else {
8 total += Math.min(tickets[i], tickets[k] - 1);
9 }
10 }
11 return total;
12 }
13}1class Solution {
2public:
3 int timeRequiredToBuy(vector<int>& tickets, int k) {
4 int total = 0;
5 for (int i = 0; i < tickets.size(); i++) {
6 if (i <= k) {
7 total += min(tickets[i], tickets[k]);
8 } else {
9 total += min(tickets[i], tickets[k] - 1);
10 }
11 }
12 return total;
13 }
14};Time and space complexity
O(n)
Each person is looked at once and contributes a constant-time min.
O(1)
Only the running total is stored.
Other approaches
Queue simulation (time O(n · max(tickets)), space O(n))
Keep a queue of indexes. Each second, pop the front, decrement their count, add one to the clock, and push them back if they still need tickets. Stop when person k reaches zero. It mirrors the statement exactly and is fine for these limits.
Round-by-round sweep (time O(n · tickets[k]), space O(1))
Loop over the array repeatedly, decrementing every positive count once per pass and adding a second for each, until person k hits zero. It avoids a queue object but does the same amount of work as the simulation.
Common mistakes
- Using
tickets[k]as the cap for people behind k. They never get to buy in the round where k finishes, so their cap istickets[k] - 1. - Writing
i < kinstead ofi <= kand then giving person k the smaller cap, which undercounts by one. - Simulating with a queue and checking for completion only after a full round, which overshoots by the people behind k.
- Forgetting that people who want fewer tickets than the cap leave early and only contribute what they actually bought.
Frequently asked questions
Why can people behind k buy only tickets[k] - 1 tickets?
Person k leaves the moment they buy their last ticket in round tickets[k]. Everyone behind them in that round would have bought after that moment, so those purchases do not count.
Does the order of the line change over time?
The relative order of the people still waiting never changes. Going to the back of the line puts a person after everyone else, which is exactly where they would be in the next round anyway.
Is simulation acceptable in an interview?
With n and tickets[i] at most 100 the simulation passes easily, and it is a fine first answer. The counting version is the follow-up that shows you understand the round structure.