首页 > 其他 > 详细

leetcode1343

时间:2020-02-09 10:32:28      阅读:70      评论:0      收藏:0      [点我收藏+]
 1 class Solution:
 2     def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
 3         n = len(arr)
 4         count = 0
 5         sums = sum(arr[:k])
 6         avg = sums // k
 7         if avg >= threshold:
 8             count += 1
 9         i,j = 0,k-1
10         while i <= j and j < n-1:
11             j += 1
12             sums += arr[j]
13             sums -= arr[i]
14             i += 1
15             avg = sums // k
16             if avg >= threshold:
17                 count += 1
18         return count

算法思路:滑动窗口,双指针。

计算窗口内部的平均值,与阈值比较。

leetcode1343

原文:https://www.cnblogs.com/asenyang/p/12286202.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!