Leetcode 936 Solution

This article provides solution to leetcode question 936 (rle-iterator)

https://leetcode.com/problems/rle-iterator

Solution

class RLEIterator:

    def __init__(self, A: List[int]):
        self.A = A
        self.i = 0
        self.offset = 0

    def next(self, n: int) -> int:
        while self.i < len(self.A) and n + self.offset > self.A[self.i]:
            n -= self.A[self.i] - self.offset
            self.i += 2
            self.offset = 0

        if self.i == len(self.A):
            return -1

        self.offset += n
        return self.A[self.i + 1]



# Your RLEIterator object will be instantiated and called as such:
# obj = RLEIterator(A)
# param_1 = obj.next(n)