I have been working this leet code questions
https://leetcode.com/problems/maximum-average-subarray-i/description/
I have been able to create a solution after understanding the sliding window algorithm. I was wondering with my code where my logic is going wrong, I do think my my issue seems to be in this section section of the code, but I am unable to pinpoint why.
while temp > k:
temp -= nums[left]
left += 1
ans = temp / (curr - left + 1)
While I do appreciate other solutions and ways solving this problem, I want to understand and get solution working first before I start looking at different ways of doing the problem, this way i get a better understanding of the algorithm.
Full code reference
def findMaxAverage(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: float
"""
left = 0
ans = 0
temp = 0
for curr in range(len(nums)):
temp += nums[curr]
curr += 1
while temp > k:
temp -= nums[left]
left += 1
ans = temp / (curr - left + 1)
return ans
tempwithk? They are completely unrelated.tempis the ongoing sum of values from thearray, andkis merely a count of how many values to use. Comparing them to each other makes no sense. It's like comparing a person's height to the temperature outside.