Leetcode 643. Maximum Average Subarray I -Google Interview Question Solved Sliding Window Approach

preview_player
Показать описание
If you find this video helpful, please 'Like' or 'Subscribe'.

Leetcode 643. Maximum Average Subarray I
LintCode 868 Maximum Average Subarray

Google Interview Question Solved Sliding Window Approach - Time Complexity O(N) Space Complexity O(1)

Link To The Problem:

Problem Description:
Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. You need to output the maximum average value.

#coding #softwareengineer #Leetcode #Algorithm #DataStructure #Java #programmer #tech #software #codinglife #Preparation
#interview #programminglife #programmingisfun #slidingwindow
Рекомендации по теме
Комментарии
Автор

I tried to base my solution on your solution but i still cant seem to get it right, can someone please tell me what i am doing wrong im passing 112/127 test cases
class Solution {
public double findMaxAverage(int[] nums, int k) {
int L = 0, R = 1;
double avg = 0.0;
int N = nums.length;
double max = 0;
if (k == 1 && N == 1) {
return nums[0];
}
int sum = 0;
for (int i = 0; i < N;i++) {
if (L == N - k + 1) {
break;
}
if (i - L + 1 > k) {
sum -= nums[L];
L++;
}
sum += nums[i];
avg = sum / (double)k;
max = Math.max(max, avg);

}
return max;

}
}

Rob-J-BJJ
welcome to shbcf.ru