Description
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
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.1 <= k <= n <= 30,000.
2.Elements of the given array will be in the range [-10,000, 10,000].
Solution
|
|
Result
AC
Analyse
考虑了几种边界情况,但是略显复杂吧,时间复杂度O(n * k),空间复杂度O(1)。标准答案里写的会更短,但是意思一样:
Optimization
First
|
|
Analyse
这个方法很精妙,就是新建了一个sums数组,这样初始值好定,接下来的比较也很容易,易于思考,但是这样的话,虽然时间复杂度变成了O(n),但是空间复杂度也成了O(n),因为建立了一个n大小的数组。
Second
|
|
Analyse
一样的思路,但是这个空间复杂度降低到了O(1),这个方法应该是最优的。但是这里要注意一个问题,在开始写这个思路的时候,我是这么写的:
这样的方法是存在问题的,比如说数组是int[] nums = {0,4,0,3,2};k = 1;这种情况下,你走到i = 2的时候,最大的还是4,但是当i = 3,nums[i] - nums[i - 1] = 3,这样就得到了7,这个是不对的,你得重新申请一个数来保存最终结果进行更新。