Using stack


  • 0
    V

    '''
    class MovingAverage(object):

    def __init__(self, size):
        """
        Initialize your data structure here.
        :type size: int
        """
        self.sta=[]
        self.size=size
        
    
    def next(self, val):
        """
        :type val: int
        :rtype: float
        """
        if len(self.sta)>=self.size:
            self.sta.pop(0)
        self.sta.append(val)
        return sum(self.sta)/float(len(self.sta))
    

    '''


Log in to reply
 

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