-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmoving-average-from-data-stream.py
142 lines (115 loc) · 3.42 KB
/
moving-average-from-data-stream.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
# https://baihuqian.github.io/2018-08-09-moving-average-from-data-stream/
# https://www.cnblogs.com/grandyang/p/5450001.html
346. Moving Average from Data Stream
Easy
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Implement the MovingAverage class:
MovingAverage(int size) Initializes the object with the size of the window size.
double next(int val) Returns the moving average of the last size values of the stream.
Example 1:
Input
["MovingAverage", "next", "next", "next", "next"]
[[3], [1], [10], [3], [5]]
Output
[null, 1.0, 5.5, 4.66667, 6.0]
Explanation
MovingAverage movingAverage = new MovingAverage(3);
movingAverage.next(1); // return 1.0 = 1 / 1
movingAverage.next(10); // return 5.5 = (1 + 10) / 2
movingAverage.next(3); // return 4.66667 = (1 + 10 + 3) / 3
movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
Constraints:
1 <= size <= 1000
-105 <= val <= 105
At most 104 calls will be made to next.
"""
# V0
# IDEA : pop, append op
# python 3
class MovingAverage(object):
def __init__(self, size):
self.size = size
self.stack = []
self.cur = 0
def next(self, val):
if self.cur == self.size:
self.stack.pop(0)
self.cur -= 1
self.stack.append(val)
self.cur += 1
return sum(self.stack) / self.cur
# V0
# IDEA : deque
from collections import deque
class MovingAverage(object):
def __init__(self, size):
self.size = size
self.q = deque([])
def next(self, val):
self.q.append(val)
self.size += 1
if len(self.q) > self.size:
self.q.pop(0)
return sum(self.q) / len(self.q)
# V0'
# IDEA : deque
from collections import deque
class MovingAverage(object):
def __init__(self, size):
self.__size = size
self.__q = deque([])
def next(self, val):
if len(self.__q) == self.__size:
self.__q.popleft()
self.__q.append(val)
return 1.0 * sum(self.__q) / len(self.__q)
# V1
# https://www.cnblogs.com/lightwindy/p/9736836.html
# https://nifannn.github.io/2018/10/01/Algorithm-Notes-Leetcode-346-Moving-Average-from-Data-Stream/
from collections import deque
class MovingAverage(object):
def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.__size = size
self.__sum = 0
self.__q = deque([])
def next(self, val):
"""
:type val: int
:rtype: float
"""
if len(self.__q) == self.__size:
self.__sum -= self.__q.popleft()
self.__sum += val
self.__q.append(val)
return 1.0 * self.__sum / len(self.__q)
# V2
# Time: O(1)
# Space: O(w)
from collections import deque
class MovingAverage(object):
def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.__size = size
self.__sum = 0
self.__q = deque([])
def next(self, val):
"""
:type val: int
:rtype: float
"""
if len(self.__q) == self.__size:
self.__sum -= self.__q.popleft()
self.__sum += val
self.__q.append(val)
return 1.0 * self.__sum / len(self.__q)
# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)