There are n people in a queue. Person i needs tickets[i] tickets, buying one per second. After buying a ticket, they go to the end of the line instantly; if they’re done, they leave.
Return the total seconds until the person originally at index k finishes buying all their tickets.
Key formula (O(n)):
Let t = tickets[k]. Then the answer is
People at or before k get served t times while k is present; those after k get at most t-1 turns before k’s last ticket.
Input: tickets = [2,3,2], k = 2
Output: 6
Input: tickets = [5,1,1,1], k = 0
Output: 8
Input: tickets = [1], k = 0
Output: 1
Accepted:
Submission: