鄂尔多斯市住房和城乡建设厅网站,帮别人做网站赚钱6,网站托管好吗,药品推荐网站模板题目#xff1a;
给你一个整数数组 arr 和两个整数 k 和 threshold 。
请你返回长度为 k 且平均值大于等于 threshold 的子数组数目。
思路#xff1a;定长滑动窗口 入 更新 出
代码#xff1a;
class Solution {public int numOfSubarrays(int[] arr, int k, int t…题目
给你一个整数数组 arr 和两个整数 k 和 threshold 。
请你返回长度为 k 且平均值大于等于 threshold 的子数组数目。
思路定长滑动窗口 入 更新 出
代码
class Solution {public int numOfSubarrays(int[] arr, int k, int threshold) {int n arr.length;int ans 0;int sum 0;for (int i 0; i n; i) {sum arr[i];if (i k - 1)continue;ans sum / k threshold ? 1 : 0;sum - arr[i - k 1];}return ans;}
}
性能
时间复杂度on
空间复杂度o1