Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
leetcode
/
algorithms
/
python
/
MaximumAverageSubarrayI
/
findMaxAverage.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
22 lines (17 loc) · 532 Bytes
master
Breadcrumbs
leetcode
/
algorithms
/
python
/
MaximumAverageSubarrayI
/
findMaxAverage.py
Copy path
Top
File metadata and controls
Code
Blame
22 lines (17 loc) · 532 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Method 1: sliding window
def findMaxAverage(self, nums, k):
total = 0
temp = float('-inf')
for i, n in enumerate(nums):
total += n
if i >= k: total -= nums[i- k]
if i >= k - 1:
temp = max(temp, total)
return temp / k
# Method 2: prefix sum
def findMaxAverage(self, nums, k):
temp = [0]
for n in nums:
temp.append(temp[-1] + n)
res = max(temp[i + k] - temp[i] for i in range(len(nums) - k + 1))
return res / k
You can’t perform that action at this time.