Time Needed to Buy Tickets
There are n people in a line to buy tickets. Each person wants to buy a certain number of tickets, represented by tickets[i]. The ticket counter sells one ticket at a time, and each person takes 1 second to buy one ticket. After buying a ticket, if a person still needs more tickets, they go to the back of the line.
You are the person at position k (0-indexed). Calculate how many seconds it takes for you to finish buying all your tickets.
Input
tickets: a list of integers wheretickets[i]is the number of tickets person i wants to buyk: an integer representing your position in line (0-indexed)
Output
An integer representing the total time in seconds for person k to finish buying all their tickets
Examples
####Example 1:
Input: tickets = [2, 3, 2], k = 2
Output: 6
Explanation:
- Time 1: Person 0 buys, queue = [3, 2, 1]
- Time 2: Person 1 buys, queue = [2, 2, 1]
- Time 3: Person 2 (you) buys, queue = [2, 1, 1]
- Time 4: Person 0 buys, queue = [1, 1, 0]
- Time 5: Person 1 buys, queue = [1, 0, 0]
- Time 6: Person 2 (you) buys and finishes
Example 2:
Input: tickets = [5, 1, 1, 1], k = 0
Output: 8
Explanation:
- You need 5 tickets and are at position 0
- After the first round, everyone else is done (they only needed 1 ticket each)
- You still need 4 tickets: times 5, 6, 7, 8
Example 3:
Input: tickets = [1], k = 0
Output: 1
Explanation: Only you in line, takes 1 second to buy your ticket