2024/10/03 灵神题单 滑动窗口

This commit is contained in:
Cool 2024-10-04 17:28:54 +08:00
parent d31333ddde
commit 8f10ed43a4
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.cool.ling_cha_mount.sliding_windows;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/04/17:28
* @Description: 1343. 大小为 K 且平均值大于等于阈值的子数组数目
* DayNumber 1
* Hard 2
* Level 3
* Score 1317
*/
public class Num1343 {
public int numOfSubarrays(int[] arr, int k, int threshold) {
int res=0;
int sum=0;
for(int i=0;i<k;i++){
sum+=arr[i];
}
if(sum>=k*threshold){
res++;
}
for(int i=k;i<arr.length;i++){
sum-=arr[i-k];
sum+=arr[i];
if(sum>=k*threshold){
res++;
}
}
return res;
}
}