Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

For example,

MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
public class MovingAverage {

    /** Initialize your data structure here. */
    Queue<Integer> queue;
    int size;
    double average;
    
    public MovingAverage(int size) {
        queue = new LinkedList<>();
        this.size = size;
        average = 0;
    }
    
    public double next(int val) {
        if (queue.size() < size) {
            average = (average * queue.size() + val) / (queue.size() + 1);
            queue.offer(val);
        } else {
            int out = queue.poll();
            queue.offer(val);
            average = (average * queue.size() - out + val) / queue.size();
        }
        return average;
    }
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */
Advertisements