Leetcode 1445 Solution

This article provides solution to leetcode question 1445 (number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold)

https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold

Solution

class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: i = k - 1 s = sum(arr[:k - 1])
ans = 0 while i < len(arr): s += arr[i]
if s >= threshold * k: ans += 1
s -= arr[i - (k - 1)] i += 1
return ans