1043. Partition Array for Maximum Sum

1043. Partition Array for Maximum Sum

class Solution:
    def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int:
        n=len(arr)
        dp=[0]*(n+1)

        for i in range(1,n+1):
            m_value=0
            for j in range(1,k+1):
                if i-j>=0:
                    m_value=max(m_value,arr[i-j])
                    dp[i]=max(dp[i],dp[i-j]+m_value*j)
        
        return dp[n]

dp[i]表示数组下标i-1处能取得的最大值

可以通过k个状态转化过来

你可能感兴趣的:(leetcode)