346. Moving Average from Data Stream π
Description
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 windowsize
.double next(int val)
Returns the moving average of the lastsize
values of the stream.
Example 1:
Input ["MovingAverage", "next", "next", "next", "next"] [[3], [1], [10], [3], [5]] Output [null, 1.0, 5.5, 4.66667, 6.0] Explanation MovingAverage movingAverage = new MovingAverage(3); movingAverage.next(1); // return 1.0 = 1 / 1 movingAverage.next(10); // return 5.5 = (1 + 10) / 2 movingAverage.next(3); // return 4.66667 = (1 + 10 + 3) / 3 movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
Constraints:
1 <= size <= 1000
-105 <= val <= 105
- At most
104
calls will be made tonext
.
Solutions
Solution 1: Circular Array
We define a variable \(\textit{s}\) to calculate the sum of the last \(\textit{size}\) elements, and a variable \(\textit{cnt}\) to record the total number of current elements. Additionally, we use an array \(\textit{data}\) of length \(\textit{size}\) to record the value of each element at each position.
When calling the \(\textit{next}\) function, we first calculate the index \(i\) where \(\textit{val}\) should be stored, then update the sum \(s\), set the value at index \(i\) to \(\textit{val}\), and increment the element count by one. Finally, we return the value of \(\frac{s}{\min(\textit{cnt}, \textit{size})}\).
The time complexity is \(O(1)\), and the space complexity is \(O(n)\), where \(n\) is the integer \(\textit{size}\) given in the problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Solution 2: Queue
We can use a queue \(\textit{q}\) to store the last \(\textit{size}\) elements, and a variable \(\textit{s}\) to record the sum of these \(\textit{size}\) elements.
When calling the \(\textit{next}\) function, we first check if the length of the queue \(\textit{q}\) is equal to \(\textit{size}\). If it is, we dequeue the front element of the queue \(\textit{q}\) and update the value of \(\textit{s}\). Then we enqueue \(\textit{val}\) and update the value of \(\textit{s}\). Finally, we return the value of \(\frac{s}{\text{len}(q)}\).
The time complexity is \(O(1)\), and the space complexity is \(O(n)\), where \(n\) is the integer \(\textit{size}\) given in the problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|