LeetCode Solutions
900. RLE Iterator
Time: Space:
class RLEIterator {
public:
RLEIterator(vector<int>& A) : A(A) {}
int next(int n) {
while (index < A.size() && A[index] < n) {
n -= A[index];
index += 2;
}
if (index == A.size())
return -1;
A[index] -= n;
return A[index + 1];
}
private:
int index = 0;
vector<int> A;
};
class RLEIterator {
public RLEIterator(int[] A) {
this.A = A;
}
public int next(int n) {
while (index < A.length && A[index] < n) {
n -= A[index];
index += 2;
}
if (index == A.length)
return -1;
A[index] -= n;
return A[index + 1];
}
private int index = 0;
private int[] A;
}
class RLEIterator:
def __init__(self, A: List[int]):
self.A = A
self.index = 0
def next(self, n: int) -> int:
while self.index < len(self.A) and self.A[self.index] < n:
n -= self.A[self.index]
self.index += 2
if self.index == len(self.A):
return -1
self.A[self.index] -= n
return self.A[self.index + 1]