# LEETCODE 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold 解题思路分析

```输入：arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4

```输入：arr = [1,1,1,1,1], k = 1, threshold = 0

```输入：arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5

```输入：arr = [7,7,7,7,7,7,7], k = 7, threshold = 7

```输入：arr = [4,4,4,4], k = 4, threshold = 1

• 1 <= arr.length <= 10^5
• 1 <= arr[i] <= 10^4
• 1 <= k <= arr.length
• 0 <= threshold <= 10^4

```public int numOfSubarrays(int[] arr, int k, int threshold) {
int sum = 0; // 区间和
// 计算前k个元素区间和
for(int i=0;i<k;i++) sum+=arr[i];
// 满足条件的最小区间和
int minSum = k * threshold;
// 返回结果
int res=0;
// 如果区间和大于最小区间和，结果加一
if(sum>=minSum) res++;
// 不断向右移动区间
int left=0,right=left+k;
while(right<arr.length){
sum-=arr[left];
sum+=arr[right];
if(sum>=minSum) res++;
left++;
right++;
}
return res;
}```

Runtime: 2 ms, faster than 97.95% of Java online submissions for Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold.

Memory Usage: 56.7 MB, less than 100.00% of Java online submissions for Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold.