Leetcode Problem 900. RLE Iterator

900. RLE Iterator

Leetcode Solutions

Iterative Approach with Internal State Tracking

  1. Initialize the RLEIterator with the encoded array and set the current index to 0.
  2. When next is called with n, iterate over the encoding array starting from the current index.
  3. If the count at the current index is 0 or less than n, subtract the count from n and move to the next element (increment the index by 2).
  4. If the count at the current index is greater than or equal to n, subtract n from the count and return the value at the current index + 1.
  5. If the end of the encoding is reached before exhausting n elements, return -1.
UML Thumbnail

Binary Search Approach with Prefix Sum Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...