Skip to content

Commit d0b7010

Browse files
authored
Update maximum_average_subarray.py
1 parent d1aa530 commit d0b7010

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

maximum_average_subarray.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Solution:
66
# @return {double} the maximum average
77
def maxAverage(self, nums, k):
88
# Write your code here
9+
# 智商不足,网上搜的答案想了很久才想明白。希望后面的注释有用!
910
self.min = sys.maxsize # 平均数的下限,不可能比最小的小。
1011
self.max = -self.min # 平均数的上限,不可能比最大的大。
1112
for i in nums:
@@ -27,7 +28,7 @@ def _search(self, nums, k, mid):
2728
for i in range(1, len(sums)):
2829
# sums[i] = (nums[0] + ... + nums[i - 1]) - (mid * i)
2930
sums[i] = sums[i - 1] + nums[i - 1] - mid
30-
if i > k:
31+
if i >= k:
3132
'''
3233
这里首先看sums[i] < 0的情况(因为bar的初始值为0)。
3334
- 如果sums[i]小于bar,第一次意味着前k个数的平均数都小于mid。

0 commit comments

Comments
 (0)