代码拉取完成,页面将自动刷新
# Time: O(n * k)
# Space: O(k)
class Solution(object):
def maxSumAfterPartitioning(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
W = K+1
dp = [0]*W
for i in xrange(len(A)):
curr_max = 0
# dp[i % W] = 0; # no need in this problem
for k in xrange(1, min(K, i+1) + 1):
curr_max = max(curr_max, A[i-k+1])
dp[i % W] = max(dp[i % W], (dp[(i-k) % W] if i >= k else 0) + curr_max*k)
return dp[(len(A)-1) % W]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。