The Time Needed to Buy Tickets

Given a queue of people and the number of tickets each person needs, return the total time for person k to finish buying tickets. Pattern focus: Queue for Task Simulation. Process the queue in rounds and stop when the target person completes all of their tickets.

Input Format

tickets = tickets needed by each person, k = target index

Output Format

time until person k finishes buying tickets

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • tickets, k must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

tickets = [2,3,2]
k = 2

Output:

6

Explanation:

The target person finishes after six time units.

Example 2:

Input:

tickets = [5,1,1,1]
k = 0

Output:

8

Explanation:

The queue cycles until the first person completes their tickets.

Loading...
The Time Needed to Buy Tickets - Queue Deque