classMovingAverage:def__init__(self,size:int):self.arr=[0]*sizeself.s=0self.cnt=0defnext(self,val:int)->float:idx=self.cnt%len(self.arr)self.s+=val-self.arr[idx]self.arr[idx]=valself.cnt+=1returnself.s/min(self.cnt,len(self.arr))# Your MovingAverage object will be instantiated and called as such:# obj = MovingAverage(size)# param_1 = obj.next(val)
1 2 3 4 5 6 7 8 91011121314151617181920212223
classMovingAverage{privateint[]arr;privateints;privateintcnt;publicMovingAverage(intsize){arr=newint[size];}publicdoublenext(intval){intidx=cnt%arr.length;s+=val-arr[idx];arr[idx]=val;++cnt;returns*1.0/Math.min(cnt,arr.length);}}/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */
1 2 3 4 5 6 7 8 910111213141516171819202122232425
classMovingAverage{public:MovingAverage(intsize){arr.resize(size);}doublenext(intval){intidx=cnt%arr.size();s+=val-arr[idx];arr[idx]=val;++cnt;return(double)s/min(cnt,(int)arr.size());}private:vector<int>arr;intcnt=0;ints=0;};/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage* obj = new MovingAverage(size); * double param_1 = obj->next(val); */
1 2 3 4 5 6 7 8 9101112131415161718192021222324
typeMovingAveragestruct{arr[]intcntintsint}funcConstructor(sizeint)MovingAverage{arr:=make([]int,size)returnMovingAverage{arr,0,0}}func(this*MovingAverage)Next(valint)float64{idx:=this.cnt%len(this.arr)this.s+=val-this.arr[idx]this.arr[idx]=valthis.cnt++returnfloat64(this.s)/float64(min(this.cnt,len(this.arr)))}/** * Your MovingAverage object will be instantiated and called as such: * obj := Constructor(size); * param_1 := obj.Next(val); */
Solution 2
1 2 3 4 5 6 7 8 91011121314151617
classMovingAverage:def__init__(self,size:int):self.n=sizeself.s=0self.q=deque()defnext(self,val:int)->float:iflen(self.q)==self.n:self.s-=self.q.popleft()self.q.append(val)self.s+=valreturnself.s/len(self.q)# Your MovingAverage object will be instantiated and called as such:# obj = MovingAverage(size)# param_1 = obj.next(val)
1 2 3 4 5 6 7 8 9101112131415161718192021222324
classMovingAverage{privateDeque<Integer>q=newArrayDeque<>();privateintn;privateints;publicMovingAverage(intsize){n=size;}publicdoublenext(intval){if(q.size()==n){s-=q.pollFirst();}q.offer(val);s+=val;returns*1.0/q.size();}}/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */
classMovingAverage{public:MovingAverage(intsize){n=size;}doublenext(intval){if(q.size()==n){s-=q.front();q.pop();}q.push(val);s+=val;return(double)s/q.size();}private:queue<int>q;ints=0;intn;};/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage* obj = new MovingAverage(size); * double param_1 = obj->next(val); */
1 2 3 4 5 6 7 8 910111213141516171819202122232425
typeMovingAveragestruct{q[]intsintnint}funcConstructor(sizeint)MovingAverage{returnMovingAverage{n:size}}func(this*MovingAverage)Next(valint)float64{iflen(this.q)==this.n{this.s-=this.q[0]this.q=this.q[1:]}this.q=append(this.q,val)this.s+=valreturnfloat64(this.s)/float64(len(this.q))}/** * Your MovingAverage object will be instantiated and called as such: * obj := Constructor(size); * param_1 := obj.Next(val); */