clueless coding // TODO: be smarter

LeetCode Moving Average from Data Stream: CPP Solution



TL;DR Code Solution



queue<int> que;
int max;
int current;

/** Initialize your data structure here. */
MovingAverage(int size) {
    max = size;
    current = 0;
}

double next(int val) {
    current += val;
    que.push(val);
    if (que.size() > max) {
        current -= que.front();
        que.pop();
    }
    return (double)current / que.size();
}