-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path1343.js
More file actions
35 lines (34 loc) · 767 Bytes
/
1343.js
File metadata and controls
35 lines (34 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* @lc app=leetcode.cn id=1343 lang=javascript
*
* [1343] Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
*/
// @lc code=start
/**
* @param {number[]} arr
* @param {number} k
* @param {number} threshold
* @return {number}
arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
i j
0 j(2) i=k-j-1
3, 4...
k-i-1 i(2)
*/
var numOfSubarrays = function(arr, k, threshold) {
let sum = 0;
const len = arr.length;
let count = 0;
for (let i = 0; i < len; i++) {
sum += arr[i];
if (i >= k) {
sum -= arr[i - k];
}
if (i >= k - 1 && sum / k >= threshold) {
count++;
}
}
return count;
};
// @lc code=end
// @lc code=end