25.83 %
class RLEIterator {
private int index;
private int remain;// how many remain on current index
private int[] A;
public RLEIterator(int[] A) {
this.A = A;
this.index = -2;
this.remain = 0;
}
public int next(int n) {
if (index >= A.length - 1) {
return -1;
}
while (n - remain > 0) {
n -= remain;
index += 2;
if (index >= A.length - 1) {
return -1;
}
remain = A[index];
}
// n - remain <= 0
remain = remain - n;
return A[index + 1];
}
}
No comments:
Post a Comment