643. 子数组最大平均数 I
给定 n
个整数,找出平均数最大且长度为 k
的连续子数组,并输出该最大平均数。
示例:
输入:[1,12,-5,-6,50,3], k = 4 输出:12.75 解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
提示:
- 1 <=
k
<=n
<= 30,000。 - 所给数据范围 [-10,000,10,000]。
has the maximum average value
=>
has the maximum sum(subArray)
class Solution: def findMaxAverage(self, nums: List[int], k: int) -> float: if len(nums)==1:return nums[0]/k window=sum(nums[i] for i in range(k)) maxWindow=window for i in range(1,len(nums)-k+1): window-=nums[i-1] window+=nums[i+k-1] maxWindow=max(maxWindow,window) return maxWindow/k