644 Maximum Average Subarray II
1. Question
Given an array consisting ofnintegers, find the contiguous subarray whose length is greater than or equal tokthat 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:
when length is 5, maximum average value is 10.8,
when length is 6, maximum average value is 9.16667.
Thus return 12.75.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
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?