Skip to content

Commit 9f05005

Browse files
committed
feat: add solutions to lc problem: No.0346. Moving Average from Data Stream
1 parent 9cffeb3 commit 9f05005

File tree

4 files changed

+152
-6
lines changed

4 files changed

+152
-6
lines changed

solution/0300-0399/0346.Moving Average from Data Stream/README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,76 @@ movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
4444
<li>最多调用 <code>next</code> 方法 <code>10<sup>4</sup></code> 次</li>
4545
</ul>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
5150

51+
“循环数组”实现。
52+
5253
<!-- tabs:start -->
5354

5455
### **Python3**
5556

5657
<!-- 这里可写当前语言的特殊实现逻辑 -->
5758

5859
```python
59-
60+
class MovingAverage:
61+
62+
def __init__(self, size: int):
63+
"""
64+
Initialize your data structure here.
65+
"""
66+
self.size = size
67+
self.data = [0] * size
68+
self.count = 0
69+
self.s = 0
70+
71+
def next(self, val: int) -> float:
72+
idx = self.count % self.size
73+
old_val = self.data[idx]
74+
self.data[idx] = val
75+
self.s += (val - old_val)
76+
self.count += 1
77+
return self.s / min(self.size, self.count)
78+
79+
80+
# Your MovingAverage object will be instantiated and called as such:
81+
# obj = MovingAverage(size)
82+
# param_1 = obj.next(val)
6083
```
6184

6285
### **Java**
6386

6487
<!-- 这里可写当前语言的特殊实现逻辑 -->
6588

6689
```java
67-
90+
class MovingAverage {
91+
private int size;
92+
private int[] data;
93+
private int count;
94+
private int s;
95+
96+
/** Initialize your data structure here. */
97+
public MovingAverage(int size) {
98+
this.size = size;
99+
this.data = new int[size];
100+
}
101+
102+
public double next(int val) {
103+
int idx = count % size;
104+
int oldVal = data[idx];
105+
data[idx] = val;
106+
s += (val - oldVal);
107+
++count;
108+
return s * 1.0 / Math.min(size, count);
109+
}
110+
}
111+
112+
/**
113+
* Your MovingAverage object will be instantiated and called as such:
114+
* MovingAverage obj = new MovingAverage(size);
115+
* double param_1 = obj.next(val);
116+
*/
68117
```
69118

70119
### **...**

solution/0300-0399/0346.Moving Average from Data Stream/README_EN.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,68 @@ movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
4040
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>next</code>.</li>
4141
</ul>
4242

43-
4443
## Solutions
4544

4645
<!-- tabs:start -->
4746

4847
### **Python3**
4948

5049
```python
51-
50+
class MovingAverage:
51+
52+
def __init__(self, size: int):
53+
"""
54+
Initialize your data structure here.
55+
"""
56+
self.size = size
57+
self.data = [0] * size
58+
self.count = 0
59+
self.s = 0
60+
61+
def next(self, val: int) -> float:
62+
idx = self.count % self.size
63+
old_val = self.data[idx]
64+
self.data[idx] = val
65+
self.s += (val - old_val)
66+
self.count += 1
67+
return self.s / min(self.size, self.count)
68+
69+
70+
# Your MovingAverage object will be instantiated and called as such:
71+
# obj = MovingAverage(size)
72+
# param_1 = obj.next(val)
5273
```
5374

5475
### **Java**
5576

5677
```java
57-
78+
class MovingAverage {
79+
private int size;
80+
private int[] data;
81+
private int count;
82+
private int s;
83+
84+
/** Initialize your data structure here. */
85+
public MovingAverage(int size) {
86+
this.size = size;
87+
this.data = new int[size];
88+
}
89+
90+
public double next(int val) {
91+
int idx = count % size;
92+
int oldVal = data[idx];
93+
data[idx] = val;
94+
s += (val - oldVal);
95+
++count;
96+
return s * 1.0 / Math.min(size, count);
97+
}
98+
}
99+
100+
/**
101+
* Your MovingAverage object will be instantiated and called as such:
102+
* MovingAverage obj = new MovingAverage(size);
103+
* double param_1 = obj.next(val);
104+
*/
58105
```
59106

60107
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class MovingAverage {
2+
private int size;
3+
private int[] data;
4+
private int count;
5+
private int s;
6+
7+
/** Initialize your data structure here. */
8+
public MovingAverage(int size) {
9+
this.size = size;
10+
this.data = new int[size];
11+
}
12+
13+
public double next(int val) {
14+
int idx = count % size;
15+
int oldVal = data[idx];
16+
data[idx] = val;
17+
s += (val - oldVal);
18+
++count;
19+
return s * 1.0 / Math.min(size, count);
20+
}
21+
}
22+
23+
/**
24+
* Your MovingAverage object will be instantiated and called as such:
25+
* MovingAverage obj = new MovingAverage(size);
26+
* double param_1 = obj.next(val);
27+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MovingAverage:
2+
3+
def __init__(self, size: int):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.size = size
8+
self.data = [0] * size
9+
self.count = 0
10+
self.s = 0
11+
12+
def next(self, val: int) -> float:
13+
idx = self.count % self.size
14+
old_val = self.data[idx]
15+
self.data[idx] = val
16+
self.s += (val - old_val)
17+
self.count += 1
18+
return self.s / min(self.size, self.count)
19+
20+
21+
# Your MovingAverage object will be instantiated and called as such:
22+
# obj = MovingAverage(size)
23+
# param_1 = obj.next(val)

0 commit comments

Comments
 (0)