package com.fishercoder.solutions;

import java.util.LinkedList;
import java.util.Queue;

/**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 _346 {

    class MovingAverage {

        private Queue<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) {
                q.offer(val);
                sum += val;
                return (double) sum / q.size();
            } else {
                int first = q.poll();
                sum -= first;
                q.offer(val);
                sum += val;
                return (double) sum / q.size();
            }
        }
    }
}
@@ -86,16 +86,63 @@
 ue;%0A%0A/**
+%0A * 346. Moving Average from Data Stream%0A *%0A * 
 Given a 
@@ -426,24 +426,64 @@
 ass _346 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     class Mo
@@ -493,24 +493,28 @@
 gAverage %7B%0A%0A
+    
         priv
@@ -535,32 +535,36 @@
 ger%3E q;%0A        
+  
+  
 private Long sum
@@ -565,32 +565,36 @@
 ng sum;%0A        
+   
+ 
 private int max;
@@ -603,20 +603,28 @@
         
+   
+ 
 /**%0A
+    
         
@@ -663,16 +663,20 @@
 e here.%0A
+    
         
@@ -683,24 +683,28 @@
  */%0A        
+    
 public Movin
@@ -736,16 +736,20 @@
         
+    
 q = new 
@@ -770,24 +770,28 @@
             
+ 
+   
 sum = 0L;%0A  
@@ -788,16 +788,20 @@
 m = 0L;%0A
+    
         
@@ -828,11 +828,19 @@
     
+   
+ 
 %7D%0A%0A
+    
     
@@ -869,24 +869,28 @@
 (int val) %7B%0A
+    
             
@@ -919,32 +919,36 @@
                 
+    
 q.offer(val);%0A  
@@ -953,32 +953,36 @@
                 
+    
 sum += val;%0A    
@@ -985,32 +985,36 @@
                 
+ 
+   
 return (double) 
@@ -1041,16 +1041,20 @@
         
+    
 %7D else %7B
@@ -1070,16 +1070,20 @@
         
+  
+  
 int firs
@@ -1108,24 +1108,28 @@
             
+    
 sum -= first
@@ -1138,32 +1138,36 @@
                 
+   
+ 
 q.offer(val);%0A  
@@ -1172,32 +1172,36 @@
                 
+    
 sum += val;%0A    
@@ -1188,32 +1188,36 @@
     sum += val;%0A
+    
                 
@@ -1240,32 +1240,50 @@
 sum / q.size();%0A
+                %7D%0A
             %7D%0A  
@@ -1297,8 +1297,9 @@
 %0A    %7D%0A%7D
+%0A

refactor 346