Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leetcode 1343. 大小为 K 且平均值大于等于阈值的子数组数目 #滑动窗口 #27

Open
webVueBlog opened this issue May 28, 2022 · 1 comment

Comments

@webVueBlog
Copy link
Member

image
1343. 大小为 K 且平均值大于等于阈值的子数组数目

@webVueBlog
Copy link
Member Author

1343. 大小为 K 且平均值大于等于阈值的子数组数目

/*
 * @lc app=leetcode.cn id=1343 lang=javascript
 *
 * [1343] 大小为 K 且平均值大于等于阈值的子数组数目
 */

// @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
输出:3

[2,2,2,2,5,5,5,8]
 i   j
子数组的长度为3, 平均数值 大于等于 4
 (68 ms)
 */
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant