# 346. Moving Average from Data Stream

{% embed url="<https://leetcode.com/problems/moving-average-from-data-stream/>" %}

维护长度固定的`queue`，一直返回mean

```python
class MovingAverage:

    def __init__(self, size: int):
        """
        Initialize your data structure here.
        """
        self.size = size
        self.q = collections.deque([])

    def next(self, val: int) -> float:
        if len(self.q) == self.size:
            self.q.popleft()
        self.q.append(val)
        return sum(self.q)/len(self.q)


# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)
```
