1
var RLEIterator = function (encoding) {
2
this.encoding = encoding;
3
// Pointer to count index
4
this.index = 0;
5
};
6

7
RLEIterator.prototype.next = function (n) {
8
while (n > 0) {
9
// Move to next count index when the current index is 0
10
if (this.encoding[this.index] === 0) this.index += 2;
11

12
// Too many calls to next, return -1
13
if (this.index >= this.encoding.length) return -1;
14

15
// n goes completely into count
16
if (n <= this.encoding[this.index]) {
17
this.encoding[this.index] -= n;
18
n = 0;
19
// count goes completely into n
20
} else if (n > this.encoding[this.index]) {
21
n -= this.encoding[this.index];
22
this.encoding[this.index] = 0;
23
}
24
}
25

26
return this.encoding[this.index + 1];
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0