classMovingAverage { public: /** Initialize your data structure here. */ MovingAverage(int size) { sz = size; } doublenext(int val){ if(dq.size() == sz) { sum -= dq.front(); dq.pop_front(); } sum += val; dq.push_back(val); return sum/dq.size(); } private: int sz = 0; double sum = 0; deque<double> dq; };
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage* obj = new MovingAverage(size); * double param_1 = obj->next(val); */