Description

Implement an iterator for run-length encoded sequences. next(n) exhausts n elements and returns the last.

Examples

Input:encoding = [3,8,0,9,2,5], next(2)
Output:8
Explanation:

Exhaust 2 of 3 8s, return 8.

Input:encoding = [1,7,3,2,4,1], next(5)
Output:1
Explanation:

The encoding represents: 1 occurrence of 7, 3 occurrences of 2, 4 occurrences of 1. Exhausting 5 elements: take 1 element (7), then take 3 elements (2,2,2), then take 1 element (1). The last element taken is 1.

Input:encoding = [5,3,2,4,1,6], next(6)
Output:4
Explanation:

The encoding represents: 5 occurrences of 3, 2 occurrences of 4, 1 occurrence of 6. Exhausting 6 elements: take all 5 occurrences of 3, then take 1 occurrence of 4. The last element taken is 4.

Constraints

  • 2 ≤ encoding.length ≤ 1000

Ready to solve this problem?

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