class Solution:
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
res = 0
l = 0
for r in range(k - 1, len(arr)):
sum_ = 0
for i in range(l, r + 1):
sum_ += arr[i]
if sum_ / k >= threshold:
res += 1
l += 1
return resWhere is the size of the array and is the size of the sub-array.
class Solution:
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
prefix_sum = [0] * (len(arr) + 1)
for i in range(len(arr)):
prefix_sum[i + 1] += prefix_sum[i] + arr[i]
res = l = 0
for r in range(k - 1, len(arr)):
sum_ = prefix_sum[r + 1] - prefix_sum[l]
if sum_ / k >= threshold:
res += 1
l += 1
return resWhere is the size of the array and is the size of the sub-array.
class Solution:
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
res = 0
curSum = sum(arr[:k - 1])
for L in range(len(arr) - k + 1):
curSum += arr[L + k - 1]
if (curSum / k) >= threshold:
res += 1
curSum -= arr[L]
return resWhere is the size of the array and is the size of the sub-array.
class Solution:
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
threshold *= k
res = curSum = 0
for R in range(len(arr)):
curSum += arr[R]
if R >= k - 1:
res += curSum >= threshold
curSum -= arr[R - k + 1]
return resWhere is the size of the array and is the size of the sub-array.