You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/README.md
Copy file name to clipboardexpand all lines: 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 number
Diff line number
Diff line change
@@ -37,33 +37,40 @@
37
37
38
38
## Solutions
39
39
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)$.
Copy file name to clipboardexpand all lines: 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 number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
classSolution {
2
2
public:
3
3
intnumOfSubarrays(vector<int>& arr, int k, int threshold) {
4
+
threshold *= k;
4
5
int s = accumulate(arr.begin(), arr.begin() + k, 0);
Copy file name to clipboardexpand all lines: solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/Solution.go
Copy file name to clipboardexpand all lines: solution/1300-1399/1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold/Solution.java
0 commit comments