C++ simple soultion beats 97.75%


  • 0
    class MovingAverage {
    public:
        deque<int>q;
        int window_size=0;
        double sum=0.0;
        /** Initialize your data structure here. */
        MovingAverage(int size) {
            window_size=size;
        }
        
        double next(int val) {
            sum+=val;
            q.push_back(val);
            while(q.size()>window_size) sum-=q.front(),q.pop_front();
            return sum/q.size();
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.