Python solution, queue O(1)


  • 0
    from collections import deque
    
    class MovingAverage(object):
        def __init__(self, size):
    
            self.size = size
            self.container = deque()
            self.cur = 0
    
        def next(self, val):
            if len(self.container) == self.size:
                self.cur -= self.container.popleft()
    
            self.container.append(val)
            self.cur += val
            # should ask what type of the output is required
            return self.cur * 1.0 / len(self.container)
    
    
    

Log in to reply
 

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