LeetCode 644: Maximum Average Subarray II Solution
Master LeetCode problem 644 (Maximum Average Subarray II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
644. Maximum Average Subarray II
Problem Explanation
Explanation:
The problem asks us to find the maximum average of a subarray with at least k elements within a given array. We can solve this problem using binary search.
Algorithmic Idea:
- We can perform binary search on the maximum average value.
- For each iteration of the binary search, we calculate the difference between each element in the array and the current mid value, and store it in a new array.
- Then, we calculate the prefix sum of the new array.
- Next, we calculate the minimum prefix sum of the subarray with at least k elements.
- If the minimum prefix sum is greater than or equal to zero, it means there exists a subarray with an average greater than or equal to the current mid value. In this case, we update the lower bound of the binary search.
- If the minimum prefix sum is less than zero, it means there doesn't exist a subarray with an average greater than or equal to the current mid value. In this case, we update the upper bound of the binary search.
Time Complexity:
The time complexity of this approach is O(n * log(max - min)), where n is the number of elements in the array, and max and min are the maximum and minimum values in the array.
Space Complexity:
The space complexity of this approach is O(n), where n is the number of elements in the array.
: :
Solution Code
class Solution {
public double findMaxAverage(int[] nums, int k) {
double left = Integer.MAX_VALUE, right = Integer.MIN_VALUE;
for (int num : nums) {
left = Math.min(left, num);
right = Math.max(right, num);
}
double[] diff = new double[nums.length];
while (right - left > 1e-5) {
double mid = (left + right) / 2;
double sum = 0, prev = 0;
boolean check = false;
for (int i = 0; i < nums.length; i++) {
diff[i] = nums[i] - mid;
}
for (int i = 0; i < k; i++) {
sum += diff[i];
}
if (sum >= 0) {
check = true;
}
double minSum = 0;
for (int i = k; i < nums.length; i++) {
sum += diff[i];
prev += diff[i - k];
minSum = Math.min(prev, minSum);
if (sum - minSum >= 0) {
check = true;
break;
}
}
if (check) {
left = mid;
} else {
right = mid;
}
}
return left;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 644 (Maximum Average Subarray II)?
This page provides optimized solutions for LeetCode problem 644 (Maximum Average Subarray II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 644 (Maximum Average Subarray II)?
The time complexity for LeetCode 644 (Maximum Average Subarray II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 644 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 644 in Java, C++, or Python.