Skip to content

Commit 0509513

Browse files
authored
feat: update solutions to lc problems: No.1343,2090 (#2558)
* No.1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold * No.2090.K Radius Subarray Averages
1 parent 463a28c commit 0509513

File tree

14 files changed

+299
-187
lines changed

14 files changed

+299
-187
lines changed

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,38 @@
4545

4646
### 方法一:滑动窗口
4747

48-
我们可以维护一个长度为 $k$ 的滑动窗口,窗口内的元素之和为 $s$,每次判断 $\frac{s}{k}$ 是否大于等于 $threshold$,如果大于等于,则满足条件的子数组个数加一
48+
不妨将 `threshold` 乘以 $k$,这样我们就可以直接比较窗口内的和与 `threshold` 的大小关系
4949

50-
最后返回满足条件的子数组个数即可
50+
我们维护一个长度为 $k$ 的滑动窗口,每次计算窗口内的和 $s$,如果 $s$ 大于等于 `threshold`,则答案加一
5151

52-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
52+
时间复杂度 $O(n)$,其中 $n$ 为数组 `arr` 的长度。空间复杂度 $O(1)$
5353

5454
<!-- tabs:start -->
5555

5656
```python
5757
class Solution:
5858
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
59+
threshold *= k
5960
s = sum(arr[:k])
60-
ans = int(s / k >= threshold)
61+
ans = int(s >= threshold)
6162
for i in range(k, len(arr)):
62-
s += arr[i]
63-
s -= arr[i - k]
64-
ans += int(s / k >= threshold)
63+
s += arr[i] - arr[i - k]
64+
ans += int(s >= threshold)
6565
return ans
6666
```
6767

6868
```java
6969
class Solution {
7070
public int numOfSubarrays(int[] arr, int k, int threshold) {
71+
threshold *= k;
7172
int s = 0;
7273
for (int i = 0; i < k; ++i) {
7374
s += arr[i];
7475
}
75-
int ans = s / k >= threshold ? 1 : 0;
76+
int ans = s >= threshold ? 1 : 0;
7677
for (int i = k; i < arr.length; ++i) {
7778
s += arr[i] - arr[i - k];
78-
ans += s / k >= threshold ? 1 : 0;
79+
ans += s >= threshold ? 1 : 0;
7980
}
8081
return ans;
8182
}
@@ -86,11 +87,12 @@ class Solution {
8687
class Solution {
8788
public:
8889
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
90+
threshold *= k;
8991
int s = accumulate(arr.begin(), arr.begin() + k, 0);
90-
int ans = s >= k * threshold;
92+
int ans = s >= threshold;
9193
for (int i = k; i < arr.size(); ++i) {
9294
s += arr[i] - arr[i - k];
93-
ans += s >= k * threshold;
95+
ans += s >= threshold;
9496
}
9597
return ans;
9698
}
@@ -99,16 +101,17 @@ public:
99101
100102
```go
101103
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
104+
threshold *= k
102105
s := 0
103106
for _, x := range arr[:k] {
104107
s += x
105108
}
106-
if s/k >= threshold {
109+
if s >= threshold {
107110
ans++
108111
}
109112
for i := k; i < len(arr); i++ {
110113
s += arr[i] - arr[i-k]
111-
if s/k >= threshold {
114+
if s >= threshold {
112115
ans++
113116
}
114117
}
@@ -118,11 +121,12 @@ func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
118121

119122
```ts
120123
function numOfSubarrays(arr: number[], k: number, threshold: number): number {
124+
threshold *= k;
121125
let s = arr.slice(0, k).reduce((acc, cur) => acc + cur, 0);
122-
let ans = s >= k * threshold ? 1 : 0;
126+
let ans = s >= threshold ? 1 : 0;
123127
for (let i = k; i < arr.length; ++i) {
124128
s += arr[i] - arr[i - k];
125-
ans += s >= k * threshold ? 1 : 0;
129+
ans += s >= threshold ? 1 : 0;
126130
}
127131
return ans;
128132
}

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

+23-13
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,40 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Sliding Window
41+
42+
We can multiply `threshold` by $k$, so that we can directly compare the sum within the window with `threshold`.
43+
44+
We maintain a sliding window of length $k$, and for each window, we calculate the sum $s$. If $s$ is greater than or equal to `threshold`, we increment the answer.
45+
46+
The time complexity is $O(n)$, where $n$ is the length of the array `arr`. The space complexity is $O(1)$.
4147

4248
<!-- tabs:start -->
4349

4450
```python
4551
class Solution:
4652
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
53+
threshold *= k
4754
s = sum(arr[:k])
48-
ans = int(s / k >= threshold)
55+
ans = int(s >= threshold)
4956
for i in range(k, len(arr)):
50-
s += arr[i]
51-
s -= arr[i - k]
52-
ans += int(s / k >= threshold)
57+
s += arr[i] - arr[i - k]
58+
ans += int(s >= threshold)
5359
return ans
5460
```
5561

5662
```java
5763
class Solution {
5864
public int numOfSubarrays(int[] arr, int k, int threshold) {
65+
threshold *= k;
5966
int s = 0;
6067
for (int i = 0; i < k; ++i) {
6168
s += arr[i];
6269
}
63-
int ans = s / k >= threshold ? 1 : 0;
70+
int ans = s >= threshold ? 1 : 0;
6471
for (int i = k; i < arr.length; ++i) {
6572
s += arr[i] - arr[i - k];
66-
ans += s / k >= threshold ? 1 : 0;
73+
ans += s >= threshold ? 1 : 0;
6774
}
6875
return ans;
6976
}
@@ -74,11 +81,12 @@ class Solution {
7481
class Solution {
7582
public:
7683
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
84+
threshold *= k;
7785
int s = accumulate(arr.begin(), arr.begin() + k, 0);
78-
int ans = s >= k * threshold;
86+
int ans = s >= threshold;
7987
for (int i = k; i < arr.size(); ++i) {
8088
s += arr[i] - arr[i - k];
81-
ans += s >= k * threshold;
89+
ans += s >= threshold;
8290
}
8391
return ans;
8492
}
@@ -87,16 +95,17 @@ public:
8795
8896
```go
8997
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
98+
threshold *= k
9099
s := 0
91100
for _, x := range arr[:k] {
92101
s += x
93102
}
94-
if s/k >= threshold {
103+
if s >= threshold {
95104
ans++
96105
}
97106
for i := k; i < len(arr); i++ {
98107
s += arr[i] - arr[i-k]
99-
if s/k >= threshold {
108+
if s >= threshold {
100109
ans++
101110
}
102111
}
@@ -106,11 +115,12 @@ func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
106115

107116
```ts
108117
function numOfSubarrays(arr: number[], k: number, threshold: number): number {
118+
threshold *= k;
109119
let s = arr.slice(0, k).reduce((acc, cur) => acc + cur, 0);
110-
let ans = s >= k * threshold ? 1 : 0;
120+
let ans = s >= threshold ? 1 : 0;
111121
for (let i = k; i < arr.length; ++i) {
112122
s += arr[i] - arr[i - k];
113-
ans += s >= k * threshold ? 1 : 0;
123+
ans += s >= threshold ? 1 : 0;
114124
}
115125
return ans;
116126
}

solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/Solution.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public:
33
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
4+
threshold *= k;
45
int s = accumulate(arr.begin(), arr.begin() + k, 0);
5-
int ans = s >= k * threshold;
6+
int ans = s >= threshold;
67
for (int i = k; i < arr.size(); ++i) {
78
s += arr[i] - arr[i - k];
8-
ans += s >= k * threshold;
9+
ans += s >= threshold;
910
}
1011
return ans;
1112
}

solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/Solution.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
2+
threshold *= k
23
s := 0
34
for _, x := range arr[:k] {
45
s += x
56
}
6-
if s/k >= threshold {
7+
if s >= threshold {
78
ans++
89
}
910
for i := k; i < len(arr); i++ {
1011
s += arr[i] - arr[i-k]
11-
if s/k >= threshold {
12+
if s >= threshold {
1213
ans++
1314
}
1415
}

solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/Solution.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public int numOfSubarrays(int[] arr, int k, int threshold) {
3+
threshold *= k;
34
int s = 0;
45
for (int i = 0; i < k; ++i) {
56
s += arr[i];
67
}
7-
int ans = s / k >= threshold ? 1 : 0;
8+
int ans = s >= threshold ? 1 : 0;
89
for (int i = k; i < arr.length; ++i) {
910
s += arr[i] - arr[i - k];
10-
ans += s / k >= threshold ? 1 : 0;
11+
ans += s >= threshold ? 1 : 0;
1112
}
1213
return ans;
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
3+
threshold *= k
34
s = sum(arr[:k])
4-
ans = int(s / k >= threshold)
5+
ans = int(s >= threshold)
56
for i in range(k, len(arr)):
6-
s += arr[i]
7-
s -= arr[i - k]
8-
ans += int(s / k >= threshold)
7+
s += arr[i] - arr[i - k]
8+
ans += int(s >= threshold)
99
return ans
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
function numOfSubarrays(arr: number[], k: number, threshold: number): number {
2+
threshold *= k;
23
let s = arr.slice(0, k).reduce((acc, cur) => acc + cur, 0);
3-
let ans = s >= k * threshold ? 1 : 0;
4+
let ans = s >= threshold ? 1 : 0;
45
for (let i = k; i < arr.length; ++i) {
56
s += arr[i] - arr[i - k];
6-
ans += s >= k * threshold ? 1 : 0;
7+
ans += s >= threshold ? 1 : 0;
78
}
89
return ans;
910
}

0 commit comments

Comments
 (0)