586. Time Needed to Buy Tickets

EasyStackQueueSimulation

People stand in a line; tickets[i] is how many tickets person i wants. Each second the front person buys one ticket then moves to the back if they still need more. Return the number of seconds until the person at position k finishes buying all their tickets. The input is JSON {tickets, k}.

Input: JSON {tickets, k}.

Output: Integer — the total seconds.

Examples

Example 1
Input: {"tickets":[2,3,2],"k":2}
Output: 6
Explanation: Person 2 finishes after 6 seconds.
Example 2
Input: {"tickets":[5,1,1,1],"k":0}
Output: 8
Explanation: Person 0 needs 5 rounds.
Example 3
Input: {"tickets":[1],"k":0}
Output: 1
Explanation: One ticket, one second.

Constraints

Asked by

GoogleBloombergMicrosoftAmazonMeta
Solve this problem in the editor →