bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 346. Moving Average from Data Stream

346. Moving Average from Data Stream

Leetcode Solutions

Approach: Array or List

  1. Initialize an empty list queue to store the stream values.
  2. Initialize a variable window_sum to store the sum of the last size elements.
  3. In the next(val) method, append val to queue.
  4. Add val to window_sum.
  5. If the length of queue exceeds size, subtract the oldest value from window_sum and remove it from queue.
  6. Return the moving average by dividing window_sum by the minimum of the window size or the length of queue.
UML Thumbnail

Approach: Circular Queue with Array

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR