Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold(大小为 K 且平均值大于等于阈值的子数组数)

超、凢脫俗 2022-10-23 08:07 97阅读 0赞

解题思路:

(1)滑动窗口法

  1. class Solution {
  2. public:
  3. int numOfSubarrays(vector<int>& arr, int k, int threshold) {
  4. int i=0,j=k-1;
  5. int sum = 0,count=0;
  6. for(int m=i;m<=j;m++) {
  7. sum+=arr[m];
  8. }
  9. if((float)sum/k>=threshold) count++;
  10. while(j<arr.size()-1) {
  11. i++;
  12. j++;
  13. sum=sum-arr[i-1]+arr[j];
  14. if((float)sum/k>=threshold) count++;
  15. }
  16. return count;
  17. }
  18. };

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读