Time Needed to Buy Tickets

People initially stand in index order. Person i needs tickets[i] tickets. Each second the front person buys one ticket, then rejoins the rear if more tickets…

People initially stand in index order. Person i needs tickets[i] tickets. Each second the front person buys one ticket, then rejoins the rear if more tickets are needed, or leaves otherwise. Return the elapsed seconds through original person k's final purchase. Stop immediately after that purchase, even in the middle of a round. Equal ticket demands do not merge people. Implement time_to_buy_tickets in Python or timeToBuyTickets in the other languages, returning one integer.

- 1 <= tickets.length <= 100. - 1 <= tickets[i] <= 100. - 0 <= k < tickets.length. - All values and k are strict integers; no zero-ticket person is present. - The answer is at most 10000 seconds. No input-mutation behavior is required. - Preset traces use at most 6 people with demands at most 6; coding retains the full bounds.

Example 1: input tickets = [2,3,2], k = 2; output 6. Person 2 completes their second purchase at second 6.

Example 2: input tickets = [3,3,3], k = 1; output 8. The target finishes before person 2 gets a third purchase.

Example 3: input tickets = [4,1,1], k = 0; output 6. Being initially first does not finish all four of person 0's tickets in one second.

Category: Queues. Topic: queue. Difficulty: Easy. Patterns: linear-scan.