Skip to content

Commit fcf6534

Browse files
committed
600-700/643.py
1 parent 7d1407f commit fcf6534

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

600-700/643.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.
3+
4+
Example 1:
5+
6+
Input: [1,12,-5,-6,50,3], k = 4
7+
Output: 12.75
8+
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
9+
10+
11+
Note:
12+
13+
1 <= k <= n <= 30,000.
14+
Elements of the given array will be in the range [-10,000, 10,000].
15+
'''
16+
17+
class Solution:
18+
def findMaxAverage(self, nums: List[int], k: int) -> float:
19+
tmp = 0
20+
res = float('-inf')
21+
for i, x in enumerate(nums):
22+
tmp += x
23+
if i >= k:
24+
tmp -= nums[i-k]
25+
if i >= k-1:
26+
res = max(res, tmp)
27+
res = res / k
28+
return res

0 commit comments

Comments
 (0)