Skip to content

Commit 9037622

Browse files
committed
feat: add solutions to lc problem: No.1343
No.1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
1 parent e6ccdf9 commit 9037622

File tree

6 files changed

+179
-2
lines changed

6 files changed

+179
-2
lines changed

Diff for: solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/README.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,89 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:滑动窗口**
47+
48+
我们可以维护一个长度为 $k$ 的滑动窗口,窗口内的元素之和为 $s$,每次判断 $\frac{s}{k}$ 是否大于等于 $threshold$,如果大于等于,则满足条件的子数组个数加一。
49+
50+
最后返回满足条件的子数组个数即可。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
4957

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

5260
```python
53-
61+
class Solution:
62+
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
63+
s = sum(arr[:k])
64+
ans = int(s / k >= threshold)
65+
for i in range(k, len(arr)):
66+
s += arr[i]
67+
s -= arr[i - k]
68+
ans += int(s / k >= threshold)
69+
return ans
5470
```
5571

5672
### **Java**
5773

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

6076
```java
77+
class Solution {
78+
public int numOfSubarrays(int[] arr, int k, int threshold) {
79+
int s = 0;
80+
for (int i = 0; i < k; ++i) {
81+
s += arr[i];
82+
}
83+
int ans = s / k >= threshold ? 1 : 0;
84+
for (int i = k; i < arr.length; ++i) {
85+
s += arr[i] - arr[i - k];
86+
ans += s / k >= threshold ? 1 : 0;
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
99+
int s = accumulate(arr.begin(), arr.begin() + k, 0);
100+
int ans = s >= k * threshold;
101+
for (int i = k; i < arr.size(); ++i) {
102+
s += arr[i] - arr[i - k];
103+
ans += s >= k * threshold;
104+
}
105+
return ans;
106+
}
107+
};
108+
```
61109
110+
### **Go**
111+
112+
```go
113+
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
114+
s := 0
115+
for _, x := range arr[:k] {
116+
s += x
117+
}
118+
if s/k >= threshold {
119+
ans++
120+
}
121+
for i := k; i < len(arr); i++ {
122+
s += arr[i] - arr[i-k]
123+
if s/k >= threshold {
124+
ans++
125+
}
126+
}
127+
return
128+
}
62129
```
63130

64131
### **...**

Diff for: solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/README_EN.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,72 @@
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
45+
s = sum(arr[:k])
46+
ans = int(s / k >= threshold)
47+
for i in range(k, len(arr)):
48+
s += arr[i]
49+
s -= arr[i - k]
50+
ans += int(s / k >= threshold)
51+
return ans
4452
```
4553

4654
### **Java**
4755

4856
```java
57+
class Solution {
58+
public int numOfSubarrays(int[] arr, int k, int threshold) {
59+
int s = 0;
60+
for (int i = 0; i < k; ++i) {
61+
s += arr[i];
62+
}
63+
int ans = s / k >= threshold ? 1 : 0;
64+
for (int i = k; i < arr.length; ++i) {
65+
s += arr[i] - arr[i - k];
66+
ans += s / k >= threshold ? 1 : 0;
67+
}
68+
return ans;
69+
}
70+
}
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
79+
int s = accumulate(arr.begin(), arr.begin() + k, 0);
80+
int ans = s >= k * threshold;
81+
for (int i = k; i < arr.size(); ++i) {
82+
s += arr[i] - arr[i - k];
83+
ans += s >= k * threshold;
84+
}
85+
return ans;
86+
}
87+
};
88+
```
4989
90+
### **Go**
91+
92+
```go
93+
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
94+
s := 0
95+
for _, x := range arr[:k] {
96+
s += x
97+
}
98+
if s/k >= threshold {
99+
ans++
100+
}
101+
for i := k; i < len(arr); i++ {
102+
s += arr[i] - arr[i-k]
103+
if s/k >= threshold {
104+
ans++
105+
}
106+
}
107+
return
108+
}
50109
```
51110

52111
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
4+
int s = accumulate(arr.begin(), arr.begin() + k, 0);
5+
int ans = s >= k * threshold;
6+
for (int i = k; i < arr.size(); ++i) {
7+
s += arr[i] - arr[i - k];
8+
ans += s >= k * threshold;
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
2+
s := 0
3+
for _, x := range arr[:k] {
4+
s += x
5+
}
6+
if s/k >= threshold {
7+
ans++
8+
}
9+
for i := k; i < len(arr); i++ {
10+
s += arr[i] - arr[i-k]
11+
if s/k >= threshold {
12+
ans++
13+
}
14+
}
15+
return
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int numOfSubarrays(int[] arr, int k, int threshold) {
3+
int s = 0;
4+
for (int i = 0; i < k; ++i) {
5+
s += arr[i];
6+
}
7+
int ans = s / k >= threshold ? 1 : 0;
8+
for (int i = k; i < arr.length; ++i) {
9+
s += arr[i] - arr[i - k];
10+
ans += s / k >= threshold ? 1 : 0;
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
3+
s = sum(arr[:k])
4+
ans = int(s / k >= threshold)
5+
for i in range(k, len(arr)):
6+
s += arr[i]
7+
s -= arr[i - k]
8+
ans += int(s / k >= threshold)
9+
return ans

0 commit comments

Comments
 (0)