-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_346.java
35 lines (29 loc) · 843 Bytes
/
_346.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
package com.fishercoder.solutions.firstthousand;
import java.util.Deque;
import java.util.LinkedList;
public class _346 {
public static class Solution1 {
class MovingAverage {
private Deque<Integer> q;
private Long sum;
private int max;
/*
* Initialize your data structure here.
*/
public MovingAverage(int size) {
q = new LinkedList();
sum = 0L;
max = size;
}
public double next(int val) {
if (q.size() >= max) {
int first = q.pollFirst();
sum -= first;
}
sum += val;
q.offer(val);
return (double) sum / q.size();
}
}
}
}