346. Moving Average from Data Stream
Design a data structure that supports:
MovingAverage(size): Initialize the object with a window sizesize.next(val): Appendvalto the stream and return the average of the lastsizevalues (or all values seen so far if fewer thansize).
The moving average is the arithmetic mean of the values currently in the window.
Examples
Input: [["MovingAverage","next"],[[1],[-50]]]
Output: [null,-50]
Input: [["MovingAverage","next","next"],[[2],[-39],[-26]]]
Output: [null,-39,-32.5]
Hints
Use a queue to maintain the last k values.
Keep a running sum for O(1) average computation.
Remove the oldest value when the window exceeds size k.
Related Problems
346. Moving Average from Data Stream
Design a data structure that supports: