Problem 216506 · easy · Phase 02 Linear Data Structures

Time Until You Leave the Ticket Line

queues · simulation

tickets[i] is the number of tickets the i-th person in a line wants. The clerk serves the person at the front, sells them one ticket in one second, and then that person either leaves (if they have all their tickets) or walks to the back of the line.

Return the number of seconds until the person who started at position k (0-based) has bought all their tickets.

Examples

Input:  tickets = [2, 3, 2], k = 2
Output: 6
Explanation: after 3 seconds everyone has 1 fewer ticket ([1, 2, 1]); after 3 more seconds person 2 buys their last ticket.

Input:  tickets = [5, 1, 1, 1], k = 0
Output: 8

Constraints

  • 1 <= len(tickets) <= 1000
  • 1 <= tickets[i] <= 100
  • 0 <= k < len(tickets)
  • Target: O(n * max(tickets)) simulation is fine; O(n) is possible

Goals

  • Simulate a line where people rejoin at the back
  • Track the identity of one person as the queue rotates
Starting Python…