Time Needed to Buy 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.

30:00

Time Needed to Buy Tickets
easy
Free written solution
Topics
Companies

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.lengthn == \text{tickets.length}

  • 1≤n≤1001 \leq n \leq 100

  • 1≤tickets[i]≤1001 \leq \text{tickets}[i] \leq 100

  • 0≤k<n0 \leq 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.

Variables
VariableValue
k2
tickets[k]2
DepthFunction Call
Stack empty
0/7