转载自https://blog.csdn.net/birdreamer/article/details/79850052
class Solution:
def largestSumOfAverages(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: float
"""
out = dict()
def dp(length, groups):
if (length, groups) in out:
return out[(length, groups)]
if groups == 1:
out[(length, groups)] = sum(A[:length]) / length
return out[(length, groups)]
temp = 0
out[length, groups] = 0
for i in range(length - 1, 0, -1):
temp += A[i]
out[(length, groups)] = max(out[(length, groups)], dp(i, groups - 1) + temp / (length - i))
return out[(length, groups)]
return dp(len(A), K)
利用动态规划,公式转自https://blog.csdn.net/qq_28954601/article/details/79852404