-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0346-MovingAverageFromDataStream.cs
41 lines (35 loc) · 1.12 KB
/
0346-MovingAverageFromDataStream.cs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----------------------------------------------------------------------------
// Runtime: 152ms
// Memory Usage: 33.2 MB
// Link: https://leetcode.com/submissions/detail/328774038/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0346_MovingAverageFromDataStream
{
private readonly Queue<int> queue;
private readonly int windowSize;
private double sum;
/** Initialize your data structure here. */
public _0346_MovingAverageFromDataStream(int size)
{
queue = new Queue<int>(size);
windowSize = size;
sum = 0.0;
}
public double Next(int val)
{
if (queue.Count == windowSize)
sum -= queue.Dequeue();
sum += val;
queue.Enqueue(val);
return sum / queue.Count;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.Next(val);
*/
}