Time Needed to Buy Tickets

Easy · Queue & Deque · about 10 minutes

Asked at Amazon, Google, Microsoft, Bloomberg.

tickets[i] is how many tickets person i wants. Each second, the person at the front buys one ticket; if they need more, they go to the back of the line, otherwise they leave. Return how many seconds it takes person k to finish buying.