Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def countSubarrays(self, nums: List[int], k: int) -> int:
- prefix_sum = [0]*len(nums)
- prefix_sum[0] = nums[0]
- for i in range(1, len(nums)):
- prefix_sum[i] = prefix_sum[i-1] + nums[i]
- # we will have a left and right pointer
- left = 0
- current_sum = 0
- ans = 0
- def get_window_sum(left, right):
- if left > right: return 0
- return prefix_sum[right] - prefix_sum[left] + nums[left]
- for right in range(len(nums)):
- # first calculate the window sum
- window_sum = get_window_sum(left, right)
- # keep reducing the window untill the window_sum*
- while (right >= left and window_sum*(right-left+1)>=k):
- left += 1
- window_sum = get_window_sum(left, right)
- ans += right - left + 1
- class Solution:
- def countSubarrays(self, nums: List[int], k: int) -> int:
- # we will have a left and right pointer
- left = 0
- current_sum = 0
- ans = 0
- for right in range(len(nums)):
- current_sum += nums[right]
- # keep reducing the window untill the window_sum*
- while (current_sum*(right - left + 1) >=k):
- current_sum -= nums[left]
- left += 1
- ans += right - left + 1
- return ans
Advertisement
Add Comment
Please, Sign In to add comment