Skip to content

Commit 05bc8c5

Browse files
authored
Create 1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js
1 parent c78a518 commit 05bc8c5

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} k
4+
* @param {number} threshold
5+
* @return {number}
6+
*/
7+
const numOfSubarrays = function(arr, k, threshold) {
8+
const n = arr.length
9+
const pre = Array(n).fill(0)
10+
pre[0] = arr[0]
11+
for(let i = 1; i < n; i++) {
12+
pre[i] = pre[i - 1] + arr[i]
13+
}
14+
15+
let res = 0
16+
if(pre[k - 1] / k >= threshold) res++
17+
for(let i = k; i < n; i++) {
18+
if(pre[i] - pre[i - k] >= k * threshold) res++
19+
}
20+
return res
21+
};

0 commit comments

Comments
 (0)