644 Maximum Average Subarray II
1. Question
Given an array consisting ofn
integers, find the contiguous subarray whose length is greater than or equal tok
that has the maximum average value. And you need to output the maximum average value.
Example 1:
Note:
1 <=
k
<=n
<= 10,000.Elements of the given array will be in range [-10,000, 10,000].
The answer with the calculation error less than 10^-5 will be accepted.
2. Implementation
(1) Brute Force
(2) Binary Search
思路: 这道题要我们找出长度至少为k的最大的subarray 平均数,可以用二分法慢慢地逼近这个最大的subarray 平均数
首先我们知道平均数的值一定介于数组中最小数min和最大数max之间, 所以二分搜查的范围是[min, max]
通过观察我们发现,如果一个数是最大的subarray平均数, 记为maxAvg,数组中任意一个subarray上的数减去maxAvg的累积和一定小于等于0, 即(a1 - maxAvg) + (a2 - maxAvg) + (a3 - maxAvg) + ... (an - maxAvg) <= 0. 利用这个性质,我们可以通过一个函数canBeLarger(),判断在二分查找的过程中得到的一个target和我们要找的maxAvg比是大还是小,从而判断搜索的区间
canBeLarger()的代码第一眼看不太好明白,这里要解释一下代码。我们需要三个变量, sum表示的是subarray[0, i]与target的差的累积和,preSum表示的是subarray[0, i - k]与target的差的累积和,minSum代表的是在[0, i - k]区间里最小的累积和。首先我们的目的是要在数组中找出长度至少为K的subarray,并且记录subarray上的数与target的差的累积和和0的大小关系。为了保证我们找到的subarray的长度至少为K,我们需要通过preSum记录与sum距离为k的subarray累积和。同时通过minSum我们可以知道[0, i - k]里的累积和中的最小数,如果sum >= minSum, 说明target比我们要找的maxAvg小,返回true。为什么要找[0, i - k]找到最小的累积和呢,如果最小的累积和与sum相减还是比0大,说明target一定比我们要找的maxAvg要大。
3. Time & Space Complexity
Brute Force: 时间复杂度O(n^2), 空间复杂度O(1)
Binary Search: 时间复杂度O(n * log(max - min)), 空间复杂度O(1)
Last updated
Was this helpful?