
There are n people standing in a queue to buy tickets. The i-th person wants to buy tickets[i] tickets. Each person buys one ticket per turn, then goes to the back of the queue (if they still need more). Find the total time for the person at position k (0-indexed) to finish buying all their tickets.
Constraints:
k is a valid index between 0 and n - 1.function timeRequiredToBuy(tickets: number[], k: number): number {
let numberOfTicketsNeeded = tickets[k]
let time = 0
while (tickets[k] > 0) {
tickets[0]--
let outOfQueue = tickets.shift()!
if (outOfQueue > 0) {
tickets.push(outOfQueue)
}
time++
if (k === 0 && outOfQueue === 0) {
return time
}
k = k > 0 ? --k % tickets.length : tickets.length - 1
}
return time
};