Time Needed to Buy Tickets

Easy

Description

There are n people in a queue. Each person wants to buy tickets[i] tickets. Person at front buys one ticket and moves to back. Return time for person at position k to finish.

Examples

Input:tickets = [2,3,2], k = 2
Output:6
Explanation:

Person 2 finishes at time 6.

Input:tickets = [1,1,1,1], k = 1
Output:2
Explanation:

Each person needs only 1 ticket. Person 0 buys at time 1, then person 1 (the target) buys at time 2 and finishes.

Input:tickets = [4,2,1,3], k = 3
Output:9
Explanation:

Person at index 3 needs 3 tickets. Simulating the queue: round 1 (4 steps), then person 0 buys 2nd (step 5), person 1 buys 2nd (step 6), person 3 buys 2nd (step 7), person 0 buys 3rd (step 8), person 3 buys 3rd (step 9). Person 3 finishes at time 9.

Constraints

  • 1 ≤ n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!