222. Moving Average from Data Stream

EasyDesign

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Implement the MovingAverage class: - MovingAverage(int size) Initializes the object with the size of the window size. - double next(int val) Returns the moving average of the last size values of the stream.

Examples

Input: ["MovingAverage","next","next","next","next"] [[3],[1],[10],[3],[5]]

Output: [null,1.0,5.5,4.666666666666667,6.0]

Explanation: Window size 3; each next averages the last up-to-3 values.

Constraints

  • 1 <= size <= 1000
  • -10^5 <= val <= 10^5
  • At most 10^4 calls will be made to next.
Loading...

Run checks all cases above. Submit evaluates all test cases.