Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
largest-sum-of-averages.py 824 Bytes
Copy Edit Raw Blame History
Allen Liu authored 2018-10-13 13:15 +08:00 . remove unused code
# Time: O(k * n^2)
# Space: O(n)
class Solution(object):
def largestSumOfAverages(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: float
"""
accum_sum = [A[0]]
for i in xrange(1, len(A)):
accum_sum.append(A[i]+accum_sum[-1])
dp = [[0]*len(A) for _ in xrange(2)]
for k in xrange(1, K+1):
for i in xrange(k-1, len(A)):
if k == 1:
dp[k % 2][i] = float(accum_sum[i])/(i+1)
else:
for j in xrange(k-2, i):
dp[k % 2][i] = \
max(dp[k % 2][i],
dp[(k-1) % 2][j] +
float(accum_sum[i]-accum_sum[j])/(i-j))
return dp[K % 2][-1]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search