Posted on

Description

Submission

class MovingAverage {
    int size;
    int sum;
    queue<int> q;
public:
    /** Initialize your data structure here. */
    MovingAverage(int size) {
        this->size = size;
        sum = 0;
    }
    
    double next(int val) {
        q.push(val);
        sum += val;
        if(q.size() > size) {
            sum -= q.front();
            q.pop();
        }
        return double(sum) / q.size();
    }
};

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

Leave a Reply

Your email address will not be published. Required fields are marked *