Skip to content

Commit b947eb7

Browse files
authored
Simpler solution
1 parent f5a9f86 commit b947eb7

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

javascript/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
* @return {number}
66
*/
77
var numOfSubarrays = function (arr, k, threshold) {
8-
let [leftPtr, rightPtr, result, windowSum] = [0, 0, 0, 0];
9-
while (rightPtr < arr.length) {
10-
windowSum += arr[rightPtr];
11-
if (rightPtr - leftPtr >= k - 1) {
12-
if (rightPtr - leftPtr > k - 1) {
13-
windowSum -= arr[leftPtr];
14-
leftPtr++;
15-
}
16-
if (windowSum / k >= threshold) {
17-
result++;
18-
}
19-
}
20-
rightPtr++;
8+
if (arr.length < k) return 0;
9+
let count = 0;
10+
let sum = 0;
11+
let L = 0;
12+
for (let R = 0; R < arr.length; R++) {
13+
sum += arr[R];
14+
if (R - L + 1 === k) {
15+
if (sum / k >= threshold)
16+
count += 1;
17+
sum -= arr[L];
18+
L += 1;
2119
}
22-
return result;
20+
}
21+
return count;
2322
};

0 commit comments

Comments
 (0)