leetcode1343

 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

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

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

猜你喜欢

转载自www.cnblogs.com/asenyang/p/12286202.html