0

Problem Description: Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value

My Solution:

    def findMaxAverage(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: float
        """
        
        curr = 0
        
        #get average of the first window
        for i in range(k):
            curr+=nums[i]
        ans = curr
        
        #slide window to find largest sum
        for i in range(k, len(nums)):
            curr+=nums[i]-nums[i-k]
            ans = max(curr, ans)
        
        #return the largest sum divided by k to get the max average
        return ans/k
            

When I submit my answer, the output seems to be rounded down even though I'm using a single division operator? For example, for the test case...

nums = [1,12,-5,-6,50,3], k = 4

The answer should be 12.75, but my output is 12.00

doublec
  • 9
  • 1

0 Answers0