Design a class MovingAverage that reports the average of the most recent values in a stream.
MovingAverage(size): the window holds at mostsizevalues.next(val): appendvalto the stream and return the average (a float) of the lastsizevalues, or of all values seen so far if there are fewer thansize.
Examples
Input: ops = ["MovingAverage", "next", "next", "next", "next"]
args = [[3], [1], [10], [3], [5]]
Output: [None, 1.0, 5.5, 4.666666666666667, 6.0]
Explanation: (1)/1, (1+10)/2, (1+10+3)/3, then the window drops 1: (10+3+5)/3.
Constraints
1 <= size <= 1000- At most
10**4calls tonext - Target:
O(1)per call (do not re-sum the window each time)
Goals
- Maintain a running sum alongside a bounded deque
- Return an average over fewer than `size` values while the window fills