forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (32 loc) · 707 Bytes
/
main.cpp
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
42
43
44
45
/// Source : https://leetcode.com/problems/moving-average-from-data-stream/description/
/// Author : liuyubobobo
/// Time : 2018-08-24
#include <iostream>
#include <queue>
using namespace std;
/// Using Queue
/// Time Complexity: O(1)
/// Space Complexity: O(size)
class MovingAverage {
private:
queue<int> q;
int sz, sum;
public:
/** Initialize your data structure here. */
MovingAverage(int size) {
sz = size;
sum = 0;
}
double next(int val) {
if(q.size() == sz){
sum -= q.front();
q.pop();
}
sum += val;
q.push(val);
return (double)sum / q.size();
}
};
int main() {
return 0;
}