my java O(1) solution


  • 0
    F

    public class MovingAverage {
    private int[] box;
    private int times=0;

    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        box=new int[size];
    }
    
    public double next(int val) {
        box[times%box.length]=val;
        times+=1;
        double total=0;
        for(int i:box){
                total+=i;
            }
        if(times>=box.length){
            return total/box.length;
        }
        else{
            return total/times;
        }
    }
    

    }


Log in to reply
 

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