Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findMaxAverage(self, nums: List[int], k: int) -> float:
- left = 0
- right = 0
- window_sum = 0
- max_window_sum = -float("inf")
- while right < len(nums):
- if nums[right] < k - 1:
- # keep adding until you expanded the
- # window enough
- window_sum = window_sum + nums[right]
- right += 1
- else:
- # add right most element assuming that left most
- # element is already inside the window
- window_sum = window_sum + nums[right]
- max_window_sum = max(window_sum, max_window_sum)
- # prepare for next iteration
- window_sum -= nums[left]
- left += 1
- right += 1
- return max_window_sum/(k*1.0)
Advertisement
Add Comment
Please, Sign In to add comment