Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MovingAverage {
- /** Initialize your data structure here. */
- int[] ary = new int[0];
- int cursize = 0, sum = 0, pos = 0;
- public MovingAverage(int size) {
- ary = new int[size];
- }
- public double next(int val) {
- if(cursize < ary.length) cursize++;
- sum -= ary[pos];
- sum += val;
- ary[pos] = val;
- pos = (pos + 1) % ary.length;
- return (double) sum / cursize;
- }
- }
- /**
- * Your MovingAverage object will be instantiated and called as such:
- * MovingAverage obj = new MovingAverage(size);
- * double param_1 = obj.next(val);
- */
Add Comment
Please, Sign In to add comment