There are n people in a line, and person i wants to buy tickets[i] tickets. Each person buys one ticket per second; after buying, a person who still needs more goes to the back of the line, and a person who has bought all of theirs leaves. Return the number of seconds until the person at position k has bought all of their tickets.
There are n people in a line, and person i wants to buy tickets[i] tickets. Each person buys one ticket per second; after buying, a person who still needs more goes to the back of the line, and a person who has bought all of theirs leaves. Return the number of seconds until the person at position k has bought all of their tickets.
Example 1:
Input: {"tickets":[2,3,2],"k":2}
Output: 6
Constraints:
n==tickets.length
1≤n≤100
1≤tickets[i]≤100
0≤k<n
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"tickets":[2,3,2],"k":2}
3 people wait in line; person 2 wants 2 tickets, and each purchase takes 1 second before going to the back.
person k = 2 wants 2
the line, front at left (height = tickets wanted)
2
0
3
1
2
2
seconds =
0
0
Filled blocks are tickets bought before person 2 finishes; dashed blocks are still unbought when they walk away.