Python beats 90% of Solutions


  • 0
    M
    class MovingAverage(object):
    
        def __init__(self, size):
            """
            Initialize your data structure here.
            :type size: int
            """
            from collections import deque
            
            self.total = 0.0
            self.size = size
            self.curr_size = 0
            self.q = deque()
    
        def next(self, val):
            """
            :type val: int
            :rtype: float
            """
            self.total += val
            self.q.append(val)
            if self.curr_size < self.size:
                self.curr_size += 1
                return self.total / self.curr_size
            else:        
                self.total -= self.q.popleft()
                return self.total / self.size
    

Log in to reply
 

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