Java solution without using queue


  • 0
    M

    '''
    public class MovingAverage {

    /** Initialize your data structure here. */
    int arr[];
    public MovingAverage(int size) {
        arr=new int[size];
        for (int i=0;i<size;i++){
            arr[i]=0;
        }
    }
    
    int count=0;
    int sum=0;
    public double next(int val) {
        sum-=arr[count%arr.length];
        arr[count%arr.length]=val;
        sum+=val;
        count++;
        return ((double)sum/Math.min(count,arr.length));
    }
    

    }

    /**

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

Log in to reply
 

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