You are given an integer array counts and an integer k. There are counts.length people standing in a line, indexed from 0 at the front to counts.length - 1 at the back. Person i wants to take counts[i] items in total.
Each second, the person at the front of the line takes one item. If that person still has items left to take, they immediately move to the back of the line. Otherwise they leave the line for good.
Return the second at which person k takes their last item.
Examples
Example 1
Input: counts = [2,3,2], k = 2
Output: 6
Explanation: The line is [2,3,2] with person 2 needing 2 items. After 1 second it becomes [3,2,1] (person 0 moved to the back). After 2 seconds, [2,1,2]. After 3 seconds, [1,2,1]. After 4 seconds, [2,1] (person 0 left the line, having taken all 2 items). After 5 seconds, [1,1]. After 6 seconds, [1], and person 2 has now taken their last item.
Example 2
Input: counts = [5,1,1,1], k = 0
Output: 8
Explanation: Person 0 needs 5 items and starts at the front. After 1 second the line is [1,1,1,4]. After 3 more seconds (4 seconds total) it is [4], with persons 1, 2 and 3 having left. Person 0 then needs 4 more seconds to take their remaining 4 items, for a total of 8 seconds.
Constraints
1≤counts.length≤100
1≤counts[i]≤100
0≤k<counts.length
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 2
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run