Ctrl K

Waiting in the Ticket Line

Easy
A line of n people stands at a ticket booth; the person with index 0 is at the front and the person with index n − 1 is at the back. Person i wants to buy wants[i] tickets. Each second the booth sells exactly one ticket to whoever is at the front. Right after buying, that person instantly goes to the back of the line if they still need more tickets, or leaves if they have all they wanted. Return how many seconds pass until the person who started at position k has bought all of their tickets.
Example 1
wants=[1, 3, 2], k=2 → 5

Second 1: the first person buys their only ticket and leaves. Seconds 2 and 3: the second and third people buy one each and rejoin the back. Second 4: the second person buys another. Second 5: the third person buys their last ticket, so the answer is 5.

Example 2
wants=[2, 4, 1], k=0 → 4

First everyone buys one ticket (3 seconds) and the third person leaves. In second 4 the first person buys their second ticket and is done.

Constraints
  • 1 ≤ n = wants.length ≤ 100
  • 1 ≤ wants[i] ≤ 100
  • 0 ≤ k < n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Picky Line at the Counter EasyA Deck That Reveals in Order MediumLast One in the Circle MediumTurnstile Queue HardHow Long the Light Stayed On Easy
loading Python…
Input
wants=[1, 3, 2], k=2
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.