Time Needed to Buy Tickets - Problem

Imagine you're at a movie theater where people queue up to buy tickets, but there's a catch! Each person can only buy one ticket at a time and takes exactly 1 second per purchase. If someone needs multiple tickets, they must go to the back of the line after each purchase.

You're given an array tickets where tickets[i] represents how many tickets the i-th person wants to buy. The person at position 0 is at the front, and position n-1 is at the back.

Your task: Find how long it takes for the person initially at position k to finish buying all their tickets.

Example: If tickets = [2,3,2] and k = 2, we want to know when person 2 (who needs 2 tickets) finishes buying. They start at the back, buy 1 ticket, go to the back again, then buy their final ticket.

Input & Output

example_1.py โ€” Basic Case
$ Input: tickets = [2,3,2], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: Person 2 (index 2) starts at the back and needs 2 tickets. Timeline: t=1: Person 0 buys (now needs 1), t=2: Person 1 buys (now needs 2), t=3: Person 2 buys (now needs 1), t=4: Person 0 buys (done), t=5: Person 1 buys (now needs 1), t=6: Person 2 buys (done). Answer: 6 seconds.
example_2.py โ€” Front Position
$ Input: tickets = [5,1,1,1], k = 0
โ€บ Output: 8
๐Ÿ’ก Note: Person 0 is at the front and needs 5 tickets. They buy 1 ticket, then go to the back. The pattern continues: 0โ†’1โ†’2โ†’3โ†’0โ†’1โ†’2โ†’3โ†’0 (person 0 done after buying 5th ticket). Total: 8 seconds.
example_3.py โ€” Single Ticket
$ Input: tickets = [1,1,1,1], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: Everyone needs only 1 ticket. Person 3 is last, so they wait for persons 0,1,2 to buy (3 seconds), then buy their own ticket (1 second). Total: 4 seconds.

Constraints

  • n == tickets.length
  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค tickets[i] โ‰ค 100
  • 0 โ‰ค k < n
  • All values are positive integers

Visualization

Tap to expand
Time Calculation StrategyFront People(positions 0 to k-1)Buy min(tickets[i], tickets[k])Target Person(position k)Buys tickets[k] ticketsBack People(positions k+1 to n-1)Buy min(tickets[i], tickets[k]-1)Why this works:โ€ข Front people: Can buy at most tickets[k] because we stop counting when target finishesโ€ข Back people: Can buy at most tickets[k]-1 because target gets the final turn firstโ€ข No queue simulation needed - just mathematical counting!โœ“ Time: O(n), Space: O(1)Total Time = Front + Target + Back
Understanding the Visualization
1
Setup
Identify target person at position k who needs tickets[k] tickets
2
Count Front
People ahead (positions 0 to k-1) contribute min(tickets[i], tickets[k]) each
3
Count Back
People behind (positions k+1 to n-1) contribute min(tickets[i], tickets[k]-1) each
4
Total Time
Sum = tickets[k] + front_contributions + back_contributions
Key Takeaway
๐ŸŽฏ Key Insight: Instead of simulating the entire queue process, we can mathematically calculate how many tickets each person will buy before our target person completes their purchases. This transforms an O(sum of tickets) simulation into an elegant O(n) counting solution.
Asked in
Amazon 15 Microsoft 8 Google 5 Apple 3
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen