Skip to content

Commit 636640c

Browse files
edit 346
1 parent da34f36 commit 636640c

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Your ideas/fixes/algorithms are more than welcome!
245245
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
246246
|348|[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_348.java)| O(1)|O(n) | Medium| Design
247247
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_347.java)| O(n)|O(1) | Medium| HashTable, Heap
248-
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MovingAveragefromDataStream.java)| O(1)|O(w)) | Easy| Queue
248+
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_346.java)| O(1)|O(w)) | Easy| Queue
249249
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_344.java) | O(n) |O(1) | Easy | String
250250
|343|[Integer Break](https://leetcode.com/problems/integer-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntegerBreak.java)| O(1)|O(1) | Medium| Math
251251
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_341.java)| O(n)|O(n) | Medium| Stack

src/main/java/com/fishercoder/solutions/MovingAveragefromDataStream.java

-45
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
7+
8+
For example,
9+
MovingAverage m = new MovingAverage(3);
10+
m.next(1) = 1
11+
m.next(10) = (1 + 10) / 2
12+
m.next(3) = (1 + 10 + 3) / 3
13+
m.next(5) = (10 + 3 + 5) / 3
14+
*/
15+
public class _346 {
16+
17+
class MovingAverage {
18+
19+
private Queue<Integer> q;
20+
private Long sum;
21+
private int max;
22+
23+
/**
24+
* Initialize your data structure here.
25+
*/
26+
public MovingAverage(int size) {
27+
q = new LinkedList();
28+
sum = 0l;
29+
max = size;
30+
}
31+
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();
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)