Problem 216506 · easy · Level 02 Linear Data Structures

Scoops at the Ice-Cream Van

queues · simulation

Children queue at an ice-cream van, and tickets[i] is how many scoops the child in place i wants. Serving takes one second per scoop: the vendor gives the child at the front one scoop; a child who now has every scoop they wanted walks off, and a child who still wants more goes round to the back of the queue.

Return how many seconds pass until the child who started in place k (counting from 0) gets their last scoop.

Examples

Input:  tickets = [3, 1, 2], k = 2
Output: 5
Explanation: seconds 1-3 give each child one scoop ([2, 0, 1]; child 1 is done and leaves); second 4 serves child 0, and in second 5 child 2 gets their last scoop.

Input:  tickets = [4, 2, 1, 3], k = 0
Output: 10

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…