1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public int maxSumAfterPartitioning(int[] arr, int k) { int n = arr.length; int[] dp = new int[n + 1];
for (int i = 1; i <= n; i++) { int max = Integer.MIN_VALUE; for (int j = 1; j <= Math.min(k, i); j++) { max = Math.max(max, arr[i - j]); dp[i] = Math.max(dp[i], dp[i - j] + j * max); } } return dp[n]; } }
|