/** Initialize your data structure here. */ publicMovingAverage(int size){ ints = new LinkedList<>(); capacity = size; sum = 0L; } publicdoublenext(int val){ if (ints.size() == capacity) { sum -= ints.poll(); } ints.add(val); sum += val; return (double) sum / ints.size(); } }
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */