Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9.1k
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29b56a3

Browse files
committedMar 20, 2025·
feat: add solutions to lc problem: No.0346
No.0346.Moving Average from Data Stream
1 parent f76309e commit 29b56a3

File tree

8 files changed

+274
-83
lines changed

8 files changed

+274
-83
lines changed
 

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

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,30 @@ movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
6565

6666
### 方法一:循环数组
6767

68+
我们定义一个变量 $\textit{s}$,用于计算当前最后 $\textit{size}$ 个元素的和,用一个变量 $\textit{cnt}$ 记录当前元素的总数。另外,我们用一个长度为 $\textit{size}$ 的数组 $\textit{data}$ 记录每个位置的元素对应的值。
69+
70+
调用 $\textit{next}$ 函数时,我们先计算出 $\textit{val}$ 要存放的下标 $i$,然后我们更新元素和 $s$,并且将下标 $i$ 处的值设置为 $\textit{val}$,同时将元素的个数加一。最后,我们返回 $\frac{s}{\min(\textit{cnt}, \textit{size})}$ 的值即可。
71+
72+
时间复杂度 $O(1)$,空间复杂度 $O(n)$,其中 $n$ 是题目给定的整数 $\textit{size}$。
73+
6874
<!-- tabs:start -->
6975

7076
#### Python3
7177

7278
```python
7379
class MovingAverage:
80+
7481
def __init__(self, size: int):
75-
self.arr = [0] * size
7682
self.s = 0
83+
self.data = [0] * size
7784
self.cnt = 0
7885

7986
def next(self, val: int) -> float:
80-
idx = self.cnt % len(self.arr)
81-
self.s += val - self.arr[idx]
82-
self.arr[idx] = val
87+
i = self.cnt % len(self.data)
88+
self.s += val - self.data[i]
89+
self.data[i] = val
8390
self.cnt += 1
84-
return self.s / min(self.cnt, len(self.arr))
91+
return self.s / min(self.cnt, len(self.data))
8592

8693

8794
# Your MovingAverage object will be instantiated and called as such:
@@ -93,20 +100,20 @@ class MovingAverage:
93100

94101
```java
95102
class MovingAverage {
96-
private int[] arr;
97103
private int s;
98104
private int cnt;
105+
private int[] data;
99106

100107
public MovingAverage(int size) {
101-
arr = new int[size];
108+
data = new int[size];
102109
}
103110

104111
public double next(int val) {
105-
int idx = cnt % arr.length;
106-
s += val - arr[idx];
107-
arr[idx] = val;
112+
int i = cnt % data.length;
113+
s += val - data[i];
114+
data[i] = val;
108115
++cnt;
109-
return s * 1.0 / Math.min(cnt, arr.length);
116+
return s * 1.0 / Math.min(cnt, data.length);
110117
}
111118
}
112119

@@ -123,21 +130,21 @@ class MovingAverage {
123130
class MovingAverage {
124131
public:
125132
MovingAverage(int size) {
126-
arr.resize(size);
133+
data.resize(size);
127134
}
128135

129136
double next(int val) {
130-
int idx = cnt % arr.size();
131-
s += val - arr[idx];
132-
arr[idx] = val;
137+
int i = cnt % data.size();
138+
s += val - data[i];
139+
data[i] = val;
133140
++cnt;
134-
return (double) s / min(cnt, (int) arr.size());
141+
return s * 1.0 / min(cnt, (int) data.size());
135142
}
136143

137144
private:
138-
vector<int> arr;
139-
int cnt = 0;
140145
int s = 0;
146+
int cnt = 0;
147+
vector<int> data;
141148
};
142149

143150
/**
@@ -151,22 +158,23 @@ private:
151158

152159
```go
153160
type MovingAverage struct {
154-
arr []int
155-
cnt int
156-
s int
161+
s int
162+
cnt int
163+
data []int
157164
}
158165

159166
func Constructor(size int) MovingAverage {
160-
arr := make([]int, size)
161-
return MovingAverage{arr, 0, 0}
167+
return MovingAverage{
168+
data: make([]int, size),
169+
}
162170
}
163171

164172
func (this *MovingAverage) Next(val int) float64 {
165-
idx := this.cnt % len(this.arr)
166-
this.s += val - this.arr[idx]
167-
this.arr[idx] = val
173+
i := this.cnt % len(this.data)
174+
this.s += val - this.data[i]
175+
this.data[i] = val
168176
this.cnt++
169-
return float64(this.s) / float64(min(this.cnt, len(this.arr)))
177+
return float64(this.s) / float64(min(this.cnt, len(this.data)))
170178
}
171179

172180
/**
@@ -176,6 +184,34 @@ func (this *MovingAverage) Next(val int) float64 {
176184
*/
177185
```
178186

187+
#### TypeScript
188+
189+
```ts
190+
class MovingAverage {
191+
private s: number = 0;
192+
private cnt: number = 0;
193+
private data: number[];
194+
195+
constructor(size: number) {
196+
this.data = Array(size).fill(0);
197+
}
198+
199+
next(val: number): number {
200+
const i = this.cnt % this.data.length;
201+
this.s += val - this.data[i];
202+
this.data[i] = val;
203+
this.cnt++;
204+
return this.s / Math.min(this.cnt, this.data.length);
205+
}
206+
}
207+
208+
/**
209+
* Your MovingAverage object will be instantiated and called as such:
210+
* var obj = new MovingAverage(size)
211+
* var param_1 = obj.next(val)
212+
*/
213+
```
214+
179215
<!-- tabs:end -->
180216

181217
<!-- solution:end -->
@@ -184,6 +220,12 @@ func (this *MovingAverage) Next(val int) float64 {
184220

185221
### 方法二:队列
186222

223+
我们可以使用一个队列 $\textit{q}$ 来存储最后 $\textit{size}$ 个元素,同时用一个变量 $\textit{s}$ 来记录这 $\textit{size}$ 个元素的和。
224+
225+
在调用 $\textit{next}$ 函数时,我们首先判断队列 $\textit{q}$ 的长度是否等于 $\textit{size}$,如果等于 $\textit{size}$,则将队列 $\textit{q}$ 的头部元素出队,并且更新 $\textit{s}$ 的值。然后将 $\textit{val}$ 入队,并且更新 $\textit{s}$ 的值。最后返回 $\frac{s}{\text{len}(q)}$ 的值即可。
226+
227+
时间复杂度 $O(1)$,空间复杂度 $O(n)$,其中 $n$ 是题目给定的整数 $\textit{size}$。
228+
187229
<!-- tabs:start -->
188230

189231
#### Python3
@@ -299,6 +341,35 @@ func (this *MovingAverage) Next(val int) float64 {
299341
*/
300342
```
301343

344+
#### TypeScript
345+
346+
```ts
347+
class MovingAverage {
348+
private q: number[] = [];
349+
private s: number = 0;
350+
private n: number;
351+
352+
constructor(size: number) {
353+
this.n = size;
354+
}
355+
356+
next(val: number): number {
357+
if (this.q.length === this.n) {
358+
this.s -= this.q.shift()!;
359+
}
360+
this.q.push(val);
361+
this.s += val;
362+
return this.s / this.q.length;
363+
}
364+
}
365+
366+
/**
367+
* Your MovingAverage object will be instantiated and called as such:
368+
* var obj = new MovingAverage(size)
369+
* var param_1 = obj.next(val)
370+
*/
371+
```
372+
302373
<!-- tabs:end -->
303374

304375
<!-- solution:end -->

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

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,32 @@ movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Circular Array
65+
66+
We define a variable $\textit{s}$ to calculate the sum of the last $\textit{size}$ elements, and a variable $\textit{cnt}$ to record the total number of current elements. Additionally, we use an array $\textit{data}$ of length $\textit{size}$ to record the value of each element at each position.
67+
68+
When calling the $\textit{next}$ function, we first calculate the index $i$ where $\textit{val}$ should be stored, then update the sum $s$, set the value at index $i$ to $\textit{val}$, and increment the element count by one. Finally, we return the value of $\frac{s}{\min(\textit{cnt}, \textit{size})}$.
69+
70+
The time complexity is $O(1)$, and the space complexity is $O(n)$, where $n$ is the integer $\textit{size}$ given in the problem.
6571

6672
<!-- tabs:start -->
6773

6874
#### Python3
6975

7076
```python
7177
class MovingAverage:
78+
7279
def __init__(self, size: int):
73-
self.arr = [0] * size
7480
self.s = 0
81+
self.data = [0] * size
7582
self.cnt = 0
7683

7784
def next(self, val: int) -> float:
78-
idx = self.cnt % len(self.arr)
79-
self.s += val - self.arr[idx]
80-
self.arr[idx] = val
85+
i = self.cnt % len(self.data)
86+
self.s += val - self.data[i]
87+
self.data[i] = val
8188
self.cnt += 1
82-
return self.s / min(self.cnt, len(self.arr))
89+
return self.s / min(self.cnt, len(self.data))
8390

8491

8592
# Your MovingAverage object will be instantiated and called as such:
@@ -91,20 +98,20 @@ class MovingAverage:
9198

9299
```java
93100
class MovingAverage {
94-
private int[] arr;
95101
private int s;
96102
private int cnt;
103+
private int[] data;
97104

98105
public MovingAverage(int size) {
99-
arr = new int[size];
106+
data = new int[size];
100107
}
101108

102109
public double next(int val) {
103-
int idx = cnt % arr.length;
104-
s += val - arr[idx];
105-
arr[idx] = val;
110+
int i = cnt % data.length;
111+
s += val - data[i];
112+
data[i] = val;
106113
++cnt;
107-
return s * 1.0 / Math.min(cnt, arr.length);
114+
return s * 1.0 / Math.min(cnt, data.length);
108115
}
109116
}
110117

@@ -121,21 +128,21 @@ class MovingAverage {
121128
class MovingAverage {
122129
public:
123130
MovingAverage(int size) {
124-
arr.resize(size);
131+
data.resize(size);
125132
}
126133

127134
double next(int val) {
128-
int idx = cnt % arr.size();
129-
s += val - arr[idx];
130-
arr[idx] = val;
135+
int i = cnt % data.size();
136+
s += val - data[i];
137+
data[i] = val;
131138
++cnt;
132-
return (double) s / min(cnt, (int) arr.size());
139+
return s * 1.0 / min(cnt, (int) data.size());
133140
}
134141

135142
private:
136-
vector<int> arr;
137-
int cnt = 0;
138143
int s = 0;
144+
int cnt = 0;
145+
vector<int> data;
139146
};
140147

141148
/**
@@ -149,22 +156,23 @@ private:
149156

150157
```go
151158
type MovingAverage struct {
152-
arr []int
153-
cnt int
154-
s int
159+
s int
160+
cnt int
161+
data []int
155162
}
156163

157164
func Constructor(size int) MovingAverage {
158-
arr := make([]int, size)
159-
return MovingAverage{arr, 0, 0}
165+
return MovingAverage{
166+
data: make([]int, size),
167+
}
160168
}
161169

162170
func (this *MovingAverage) Next(val int) float64 {
163-
idx := this.cnt % len(this.arr)
164-
this.s += val - this.arr[idx]
165-
this.arr[idx] = val
171+
i := this.cnt % len(this.data)
172+
this.s += val - this.data[i]
173+
this.data[i] = val
166174
this.cnt++
167-
return float64(this.s) / float64(min(this.cnt, len(this.arr)))
175+
return float64(this.s) / float64(min(this.cnt, len(this.data)))
168176
}
169177

170178
/**
@@ -174,13 +182,47 @@ func (this *MovingAverage) Next(val int) float64 {
174182
*/
175183
```
176184

185+
#### TypeScript
186+
187+
```ts
188+
class MovingAverage {
189+
private s: number = 0;
190+
private cnt: number = 0;
191+
private data: number[];
192+
193+
constructor(size: number) {
194+
this.data = Array(size).fill(0);
195+
}
196+
197+
next(val: number): number {
198+
const i = this.cnt % this.data.length;
199+
this.s += val - this.data[i];
200+
this.data[i] = val;
201+
this.cnt++;
202+
return this.s / Math.min(this.cnt, this.data.length);
203+
}
204+
}
205+
206+
/**
207+
* Your MovingAverage object will be instantiated and called as such:
208+
* var obj = new MovingAverage(size)
209+
* var param_1 = obj.next(val)
210+
*/
211+
```
212+
177213
<!-- tabs:end -->
178214

179215
<!-- solution:end -->
180216

181217
<!-- solution:start -->
182218

183-
### Solution 2
219+
### Solution 2: Queue
220+
221+
We can use a queue $\textit{q}$ to store the last $\textit{size}$ elements, and a variable $\textit{s}$ to record the sum of these $\textit{size}$ elements.
222+
223+
When calling the $\textit{next}$ function, we first check if the length of the queue $\textit{q}$ is equal to $\textit{size}$. If it is, we dequeue the front element of the queue $\textit{q}$ and update the value of $\textit{s}$. Then we enqueue $\textit{val}$ and update the value of $\textit{s}$. Finally, we return the value of $\frac{s}{\text{len}(q)}$.
224+
225+
The time complexity is $O(1)$, and the space complexity is $O(n)$, where $n$ is the integer $\textit{size}$ given in the problem.
184226

185227
<!-- tabs:start -->
186228

@@ -297,6 +339,35 @@ func (this *MovingAverage) Next(val int) float64 {
297339
*/
298340
```
299341

342+
#### TypeScript
343+
344+
```ts
345+
class MovingAverage {
346+
private q: number[] = [];
347+
private s: number = 0;
348+
private n: number;
349+
350+
constructor(size: number) {
351+
this.n = size;
352+
}
353+
354+
next(val: number): number {
355+
if (this.q.length === this.n) {
356+
this.s -= this.q.shift()!;
357+
}
358+
this.q.push(val);
359+
this.s += val;
360+
return this.s / this.q.length;
361+
}
362+
}
363+
364+
/**
365+
* Your MovingAverage object will be instantiated and called as such:
366+
* var obj = new MovingAverage(size)
367+
* var param_1 = obj.next(val)
368+
*/
369+
```
370+
300371
<!-- tabs:end -->
301372

302373
<!-- solution:end -->

‎solution/0300-0399/0346.Moving Average from Data Stream/Solution.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
class MovingAverage {
22
public:
33
MovingAverage(int size) {
4-
arr.resize(size);
4+
data.resize(size);
55
}
66

77
double next(int val) {
8-
int idx = cnt % arr.size();
9-
s += val - arr[idx];
10-
arr[idx] = val;
8+
int i = cnt % data.size();
9+
s += val - data[i];
10+
data[i] = val;
1111
++cnt;
12-
return (double) s / min(cnt, (int) arr.size());
12+
return s * 1.0 / min(cnt, (int) data.size());
1313
}
1414

1515
private:
16-
vector<int> arr;
17-
int cnt = 0;
1816
int s = 0;
17+
int cnt = 0;
18+
vector<int> data;
1919
};
2020

2121
/**

‎solution/0300-0399/0346.Moving Average from Data Stream/Solution.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
type MovingAverage struct {
2-
arr []int
3-
cnt int
4-
s int
2+
s int
3+
cnt int
4+
data []int
55
}
66

77
func Constructor(size int) MovingAverage {
8-
arr := make([]int, size)
9-
return MovingAverage{arr, 0, 0}
8+
return MovingAverage{
9+
data: make([]int, size),
10+
}
1011
}
1112

1213
func (this *MovingAverage) Next(val int) float64 {
13-
idx := this.cnt % len(this.arr)
14-
this.s += val - this.arr[idx]
15-
this.arr[idx] = val
14+
i := this.cnt % len(this.data)
15+
this.s += val - this.data[i]
16+
this.data[i] = val
1617
this.cnt++
17-
return float64(this.s) / float64(min(this.cnt, len(this.arr)))
18+
return float64(this.s) / float64(min(this.cnt, len(this.data)))
1819
}
1920

2021
/**

‎solution/0300-0399/0346.Moving Average from Data Stream/Solution.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
class MovingAverage {
2-
private int[] arr;
32
private int s;
43
private int cnt;
4+
private int[] data;
55

66
public MovingAverage(int size) {
7-
arr = new int[size];
7+
data = new int[size];
88
}
99

1010
public double next(int val) {
11-
int idx = cnt % arr.length;
12-
s += val - arr[idx];
13-
arr[idx] = val;
11+
int i = cnt % data.length;
12+
s += val - data[i];
13+
data[i] = val;
1414
++cnt;
15-
return s * 1.0 / Math.min(cnt, arr.length);
15+
return s * 1.0 / Math.min(cnt, data.length);
1616
}
1717
}
1818

‎solution/0300-0399/0346.Moving Average from Data Stream/Solution.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class MovingAverage:
2+
23
def __init__(self, size: int):
3-
self.arr = [0] * size
44
self.s = 0
5+
self.data = [0] * size
56
self.cnt = 0
67

78
def next(self, val: int) -> float:
8-
idx = self.cnt % len(self.arr)
9-
self.s += val - self.arr[idx]
10-
self.arr[idx] = val
9+
i = self.cnt % len(self.data)
10+
self.s += val - self.data[i]
11+
self.data[i] = val
1112
self.cnt += 1
12-
return self.s / min(self.cnt, len(self.arr))
13+
return self.s / min(self.cnt, len(self.data))
1314

1415

1516
# Your MovingAverage object will be instantiated and called as such:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MovingAverage {
2+
private s: number = 0;
3+
private cnt: number = 0;
4+
private data: number[];
5+
6+
constructor(size: number) {
7+
this.data = Array(size).fill(0);
8+
}
9+
10+
next(val: number): number {
11+
const i = this.cnt % this.data.length;
12+
this.s += val - this.data[i];
13+
this.data[i] = val;
14+
this.cnt++;
15+
return this.s / Math.min(this.cnt, this.data.length);
16+
}
17+
}
18+
19+
/**
20+
* Your MovingAverage object will be instantiated and called as such:
21+
* var obj = new MovingAverage(size)
22+
* var param_1 = obj.next(val)
23+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MovingAverage {
2+
private q: number[] = [];
3+
private s: number = 0;
4+
private n: number;
5+
6+
constructor(size: number) {
7+
this.n = size;
8+
}
9+
10+
next(val: number): number {
11+
if (this.q.length === this.n) {
12+
this.s -= this.q.shift()!;
13+
}
14+
this.q.push(val);
15+
this.s += val;
16+
return this.s / this.q.length;
17+
}
18+
}
19+
20+
/**
21+
* Your MovingAverage object will be instantiated and called as such:
22+
* var obj = new MovingAverage(size)
23+
* var param_1 = obj.next(val)
24+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.