LeetCode 0346. Moving Average from Data Stream Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0346. 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 window size.
  • double next(int val) Returns the moving average of the last size 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 to next.

Solutions

Solution 1: Circular Array

We define a variable s to calculate the sum of the last size elements, and a variable cnt to record the total number of current elements. Additionally, we use an array data of length size to record the value of each element at each position.

When calling the next function, we first calculate the index i where val should be stored, then update the sum s, set the value at index i to val, and increment the element count by one. Finally, we return the value of smin(cnt, size).

The time complexity is O(1), and the space complexity is O(n), where n is the integer size given in the problem.

PythonJavaC++GoTypeScript
class MovingAverage: def __init__(self, size: int): self.s = 0 self.data = [0] * size self.cnt = 0 def next(self, val: int) -> float: i = self.cnt % len(self.data) self.s += val - self.data[i] self.data[i] = val self.cnt += 1 return self.s / min(self.cnt, len(self.data)) # Your MovingAverage object will be instantiated and called as such: # obj = MovingAverage(size) # param_1 = obj.next(val)(code-box)

Solution 2: Queue

We can use a queue q to store the last size elements, and a variable s to record the sum of these size elements.

When calling the next function, we first check if the length of the queue q is equal to size. If it is, we dequeue the front element of the queue q and update the value of s. Then we enqueue val and update the value of s. Finally, we return the value of slen(q).

The time complexity is O(1), and the space complexity is O(n), where n is the integer size given in the problem.

PythonJavaC++GoTypeScript
class MovingAverage: def __init__(self, size: int): self.n = size self.s = 0 self.q = deque() def next(self, val: int) -> float: if len(self.q) == self.n: self.s -= self.q.popleft() self.q.append(val) self.s += val return self.s / len(self.q) # Your MovingAverage object will be instantiated and called as such: # obj = MovingAverage(size) # param_1 = obj.next(val)(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !