class RLEIterator {
public:
RLEIterator(vector<int>& encoding) {
}
int next(int n) {
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator* obj = new RLEIterator(encoding);
* int param_1 = obj->next(n);
*/
type RLEIterator struct {
data []int
pos int
left int
}
func Constructor(encoding []int) RLEIterator {
return RLEIterator{encoding, -2, 0}
}
func (this *RLEIterator) Next(n int) int {
for n > this.left {
this.pos += 2
if this.pos >= len(this.data) {
return -1
}
this.left += this.data[this.pos]
}
if this.pos >= len(this.data) {
return -1
}
this.left -= n
// fmt.Println(n,this.data[this.pos+1])
return this.data[this.pos+1]
}
/**
* Your RLEIterator object will be instantiated and called as such:
* obj := Constructor(A);
* param_1 := obj.Next(n);
*/
class RLEIterator {
int[] A;
int i, q;
public RLEIterator(int[] A) {
this.A = A;
i = q = 0;
}
public int next(int n) {
while (i < A.length) {
if (q + n > A[i]) {
n -= A[i] - q;
q = 0;
i += 2;
} else {
q += n;
return A[i+1];
}
}
return -1;
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(encoding);
* int param_1 = obj.next(n);
*/