Skip to content

Practice RLE Iterator

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#900
LeetCode Problem

RLE Iterator

Problem Statement

RLEIterator(encoding) receives alternating repetition counts and values. next(n) consumes the next n decoded entries and returns the last consumed value. If fewer than n entries remain, consume the remainder and return -1. Zero-length runs contribute no entries. Practice approach: Keep a run pointer and remaining count rather than expanding the sequence, because counts may reach 10^9. Skip exhausted runs while subtracting their sizes from n. A request ending exactly at a run boundary still returns that run's value. Across calls, each run is passed at most once.

Source: RLE Iterator on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["RLEIterator","next","next","next","next"]
[[[2,4,0,9,3,7]],[2],[1],[3],[1]]
Output
[null,4,7,-1,-1]
Explanation

The decoded sequence is [4,4,7,7,7]. The third request exceeds the remaining two entries and exhausts the iterator.

Constraints

2 <= encoding.length <= 1000
encoding.length is even.
0 <= encoding[i] <= 10^9
1 <= n <= 10^9
At most 1000 next calls.

Related LeetCode Problems

Practice more problems similar to RLE Iterator.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.