-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathMovingAverageFromDataStream346.java
72 lines (60 loc) · 1.9 KB
/
MovingAverageFromDataStream346.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Given a stream of integers and a window size, calculate the moving average
* of all integers in the sliding window.
*
* For example,
* MovingAverage m = new MovingAverage(3);
* m.next(1) = 1
* m.next(10) = (1 + 10) / 2
* m.next(3) = (1 + 10 + 3) / 3
* m.next(5) = (10 + 3 + 5) / 3
*/
public class MovingAverageFromDataStream346 {
class MovingAverage {
private int size;
private Queue<Integer> cache;
private long sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
this.size = size;
this.cache = new LinkedList<Integer>();
this.sum = 0L;
}
public double next(int val) {
if (this.cache.size() >= this.size) {
this.sum -= this.cache.remove();
}
this.sum += val;
this.cache.add(val);
return this.sum * 1.0 / this.cache.size();
}
}
class MovingAverage2 {
private int[] window;
private int head = 0;
private int len = 0 ;
private int sum = 0;
/** Initialize your data structure here. */
public MovingAverage2(int size) {
this.window = new int[size + 1];
}
public double next(int val) {
int nextPos = (this.head + this.len + 1) % this.window.length;
this.window[nextPos] = val;
this.len++;
this.sum += val;
if (this.len == this.window.length) {
this.head++;
this.head %= this.window.length;
this.len--;
this.sum -= this.window[this.head];
}
return this.sum * 1.0 / this.len;
}
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/