File tree Expand file tree Collapse file tree 1 file changed +30
-25
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +30
-25
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .LinkedList ;
4
4
import java .util .Queue ;
5
5
6
- /**Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
6
+ /**
7
+ * 346. Moving Average from Data Stream
8
+ *
9
+ * Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
7
10
8
11
For example,
9
12
MovingAverage m = new MovingAverage(3);
14
17
*/
15
18
public class _346 {
16
19
17
- class MovingAverage {
20
+ public static class Solution1 {
21
+ class MovingAverage {
18
22
19
- private Queue <Integer > q ;
20
- private Long sum ;
21
- private int max ;
23
+ private Queue <Integer > q ;
24
+ private Long sum ;
25
+ private int max ;
22
26
23
- /**
24
- * Initialize your data structure here.
25
- */
26
- public MovingAverage (int size ) {
27
- q = new LinkedList ();
28
- sum = 0L ;
29
- max = size ;
30
- }
27
+ /**
28
+ * Initialize your data structure here.
29
+ */
30
+ public MovingAverage (int size ) {
31
+ q = new LinkedList ();
32
+ sum = 0L ;
33
+ max = size ;
34
+ }
31
35
32
- public double next (int val ) {
33
- if (q .size () < max ) {
34
- q .offer (val );
35
- sum += val ;
36
- return (double ) sum / q .size ();
37
- } else {
38
- int first = q .poll ();
39
- sum -= first ;
40
- q .offer (val );
41
- sum += val ;
42
- return (double ) sum / q .size ();
36
+ public double next (int val ) {
37
+ if (q .size () < max ) {
38
+ q .offer (val );
39
+ sum += val ;
40
+ return (double ) sum / q .size ();
41
+ } else {
42
+ int first = q .poll ();
43
+ sum -= first ;
44
+ q .offer (val );
45
+ sum += val ;
46
+ return (double ) sum / q .size ();
47
+ }
43
48
}
44
49
}
45
50
}
46
- }
51
+ }
You can’t perform that action at this time.
0 commit comments