n people stands at a ticket booth; the person with index 0 is at the front and the person with index n − 1 is at the back. Person i wants to buy wants[i] tickets.
Each second the booth sells exactly one ticket to whoever is at the front. Right after buying, that person instantly goes to the back of the line if they still need more tickets, or leaves if they have all they wanted.
Return how many seconds pass until the person who started at position k has bought all of their tickets.wants=[1, 3, 2], k=2 → 5
Second 1: the first person buys their only ticket and leaves. Seconds 2 and 3: the second and third people buy one each and rejoin the back. Second 4: the second person buys another. Second 5: the third person buys their last ticket, so the answer is 5.
wants=[2, 4, 1], k=0 → 4
First everyone buys one ticket (3 seconds) and the third person leaves. In second 4 the first person buys their second ticket and is done.
n = wants.length ≤ 100wants[i] ≤ 100k < nwants=[1, 3, 2], k=2
5
Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.