#223EasySimulation· Round-Robin Queue
Ticket Counter Rounds
Input format
`tickets`: an array of positive integers; `k`: a position in the line.
Output format
An integer: the total seconds until person `k` is finished.
Examples
Input: tickets = [2, 3, 2], k = 2
Output: 6
Round 1 serves everybody once (3 seconds), leaving 1, 2, 1. Round 2 serves person 0 (finished), person 1, then person 2, who finishes at second 6.
Input: tickets = [5, 1, 1, 1], k = 0
Output: 8
Person 0 needs 5 turns. Others leave after round 1, so the line shrinks: 4 seconds in round 1, then person 0 buys alone for 4 more seconds, giving 8.
Input: tickets = [1], k = 0
Output: 1
A single person with a single ticket takes one second.
Constraints
- 1 <= tickets.length <= 10^4
- 1 <= tickets[i] <= 10^4
- 0 <= k < tickets.length
- The answer is at most 10^8.
- #simulation
- #queue
- #math
- #round-robin