05 August 2008
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
1
2
3
4
5
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
澄清返回的结果是double,实现的方式根据自己熟悉的和复杂度来,如果用array时间复杂度为O(n),因为要移动整个数组元素。
用LinkedList或者Deque,都可以进行两端的操作。
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
class MovingAverage {
LinkedList<Integer> list;
int count;
int sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
list = new LinkedList<>();
count = size;
sum = 0;
}
public double next(int val) {
if (list.size() < count) {
list.add(val);
sum += val;
return (double) sum / list.size();
} else { // list.size() == count
sum -= list.pollFirst();
list.add(val);
sum += val;
}
return (double) sum / count;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
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
class MovingAverage {
Deque<Integer> deque;
int count;
int sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
deque = new ArrayDeque<>();
count = size;
sum = 0;
}
public double next(int val) {
deque.offer(val);
if (deque.size() <= count) {
sum += val;
return (double) sum / deque.size();
} else {
sum -= deque.poll();
sum += val;
}
return (double) sum / count;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/