classMovingAverage { public: queue<int>q; int s; double cur = 0; /** Initialize your data structure here. */ MovingAverage(int size) { s = size; } doublenext(int val){ q.push(val); cur += val; if(q.size() > s){ cur -= q.front(); q.pop(); }
return cur / q.size(); } };
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage* obj = new MovingAverage(size); * double param_1 = obj->next(val); */