class MovingAverage {
int insert, count;
long sum;
int[] window;
/** Initialize your data structure here. */
public MovingAverage(int size) {
insert = 0;
count = 0;
sum = 0;
window = new int[size];
}
public double next(int val) {
if (count < window.length) {
count++;
}
sum -= window[insert];
window[insert] = val;
sum += val;
insert = (insert + 1) % window.length;
return (double)(sum) / count;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/