Skip to content

Commit aa67bff

Browse files
committed
Time: 302 ms (81.72%), Space: 16.7 MB (78.54%) - LeetHub
1 parent acef0db commit aa67bff

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxSumAfterPartitioning(self, arr, k):
3+
N = len(arr)
4+
K = k + 1
5+
6+
dp = [0] * K
7+
8+
for start in range(N - 1, -1, -1):
9+
curr_max = 0
10+
end = min(N, start + k)
11+
12+
for i in range(start, end):
13+
curr_max = max(curr_max, arr[i])
14+
dp[start % K] = max(dp[start % K], dp[(i + 1) % K] + curr_max * (i - start + 1))
15+
16+
return dp[0]

0 commit comments

Comments
 (0)