Time Needed to Buy Tickets
Easy · queue
People are standing in a line at a ticket desk. Person i wants to buy tickets[i] tickets, and k is the position of the person you care about (the first person is at position 0).
The desk works in one-minute turns. In each minute, the person at the front of the line buys exactly one ticket. If that person still wants more tickets, they walk to the back of the line to wait for another turn. If they have everything they wanted, they leave the line for good.
Return the number of minutes that pass until the person who started at position k has bought their last ticket, counting the minute in which they buy it.
Examples
Example 1
Input: tickets = [2, 3, 2], k = 2
Output: 6
The line starts as persons 0, 1, 2. Persons 0 and 2 need two tickets, so the order of turns is 0, 1, 2, 0, 1, 2. Person 2 finishes in the sixth minute.
Example 2
Input: tickets = [1, 4, 1, 1], k = 0
Output: 1
Person 0 is at the front and needs one ticket, so they finish in the first minute.
Example 3
Input: tickets = [5], k = 0
Output: 5
Only one person is in line, so they buy a ticket every minute.
Constraints
- 1 <= len(tickets) <= 1000
- 1 <= tickets[i] <= 1000
- 0 <= k < len(tickets)