|
| 1 | +# Moving Average from Data Stream |
| 2 | + |
| 3 | +Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. |
| 4 | +Implement a class called MovingAverage that has the following methods: |
| 5 | + |
| 6 | +Constructor (int size): This constructor initializes the object with the specified window size. |
| 7 | + |
| 8 | +double next (int val): This method takes an integer value as input and returns the moving average of the last size |
| 9 | +values from the stream. |
| 10 | + |
| 11 | +## Constraints |
| 12 | + |
| 13 | +- 1 <= size <= 100 |
| 14 | +- -10^3 <= val <= 10^3 |
| 15 | +- At most 10^2 calls will be made to next |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +The algorithm calculates the moving average of the most recent values within a fixed-size window. It employs a queue to |
| 24 | +store these values and maintains a running sum for efficient computation. Each time a new value is added to the window, |
| 25 | +it is appended to the queue, and the running sum is updated accordingly. If the queue exceeds the specified size (i.e., |
| 26 | +the window is full), the oldest value is removed from the queue, and the sum is adjusted by subtracting this value. The |
| 27 | +moving average is then calculated by dividing the running sum by the number of values in the queue. This approach ensures |
| 28 | +that the moving average is updated, achieving constant time complexity for each operation. |
| 29 | + |
| 30 | +1. **Constructor**: The constructor initializes the following variables: |
| 31 | + - _size_: This represents the window size, the maximum number of recent values for calculating the moving average. |
| 32 | + - _queue_: A queue data structure stores the most recent values up to the specified window size. The queue supports |
| 33 | + efficient operations for adding new values to the end and removing the oldest value from the front, essential for |
| 34 | + maintaining the sliding window. |
| 35 | + - _window_sum_: This keeps a running sum of the values currently in the window. This allows the moving average to be |
| 36 | + calculated efficiently without the need to sum all values repeatedly. |
| 37 | + |
| 38 | +2. The **next** method: The next method calculates the moving average by following these steps: |
| 39 | + - Enqueue the new value (val) to queue and add it to window_sum. This effectively extends the current sliding window |
| 40 | + to include the new value. |
| 41 | + - If the number of elements in queue exceeds size (indicating the window is full), remove the oldest value from queue |
| 42 | + and update window_sum by subtracting this value. This ensures the queue size stays within the specified window size. |
| 43 | + - Compute the moving average as window_sum / len(queue). Use floating-point division to ensure the result is a float. |
| 44 | + |
| 45 | +### Time Complexity |
| 46 | + |
| 47 | +1. **Constructor**: Initializing the MovingAverage instance with a given size takes O(1) time. This is because the operation |
| 48 | + involves only setting the size, initializing an empty queue, and initializing the sum. |
| 49 | +2. **Next method**: |
| 50 | + - Appending a value: Adding a value to the queue takes O(1) time as operations like append and popleft are optimized |
| 51 | + for constant time execution in a queue. |
| 52 | + - Removing the oldest value: If the window is full, removing the oldest value also takes O(1) time, as popleft is a |
| 53 | + constant time operation in a queue. |
| 54 | + - Calculating the moving average: This step involves dividing window_sum by the number of elements in the queue, |
| 55 | + which is an O(1) operation. |
| 56 | + |
| 57 | +Thus the time complexity of the next method is O(1). |
| 58 | + |
| 59 | +### Space Complexity |
| 60 | + |
| 61 | +The queue holds up to _size_ elements at any given time (i.e., the number of elements in the sliding window). Therefore, |
| 62 | +the space complexity of the queue is O(size), where size is the maximum window size. |
0 commit comments