Java


  • 0
    J

    class MovingAverage {
    private int maxSize;
    private double sum;
    private List nodes;

    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        nodes = new ArrayList<Integer>();
        maxSize = size;
    }
    
    public double next(int val) {
        nodes.add(val);
        if (nodes.size() > maxSize) {
            int first = (int) nodes.get(0);
            sum -= first;
            nodes.remove(0);
        }
        return (sum += val) / nodes.size();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.