难度:easy
Given an array consisting of n
integers, find the contiguous subarray of given length k
that has the maximum average value. And you need to output the maximum average value.
Example 1:
Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
- 1 <=
k
<=n
<= 30,000. - Elements of the given array will be in the range [-10,000, 10,000].
思路:求给定list里面一段长度为K的片段,使得该片段的具有最大平均值。
遍历全表,找到所有可能的长度为K的片段,求其最大平均值。
关于ans的初始值设定,开始设为0,或者None 均为错误,于是设置为第一个K片段的平均值。