Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MovingAverage {
- public:
- /** Initialize your data structure here. */
- MovingAverage(int size) : windowSize(size) {}
- double next(int val) {
- while(data.size() >= windowSize)
- {
- sum -= data.front();
- data.pop_front();
- }
- data.push_back(val);
- sum += val;
- return sum/double(data.size());
- }
- private:
- std::deque<double> data;
- int windowSize = 0;
- int sum = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement