
You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
n개의 원소를 가진 정수배열 nums와 정수 k가 주어졌을 때,
길이가 k이고 원소 값들의 평균이 최대인 contiguous subarray를 찾아 그 최대 평균 값(maximum average value)을 반환을 해야하는 문제이다.
Input: nums = [1,12,-5,-6,50,3], k = 4
Output: 12.75000
Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Input: nums = [5], k = 1
Output: 5.00000
class Solution:
def findMaxAverage(self, nums: List[int], k: int) -> float:
answer = curr = sum(nums[:k])
for i in range(k, len(nums)):
curr += nums[i] - nums[i - k]
answer = max(answer, curr)
return answer / k