Partition Array for Maximum Sum

medium dynamic programming array partition

Problem

Given an integer array arr, partition it into (contiguous) subarrays of length at most k. After partitioning, each subarray has all its values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.

Inputarr = [1, 4, 1, 5, 3], k = 3
Output23
Best partition is [1, 4] then [1, 5, 3] → [4, 4] + [5, 5, 5] = 8 + 15 = 23.

def max_sum_after_partitioning(arr, k):
    n = len(arr)
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        best = 0
        cur_max = 0
        for j in range(1, min(k, i) + 1):
            cur_max = max(cur_max, arr[i - j])
            best = max(best, dp[i - j] + cur_max * j)
        dp[i] = best
    return dp[n]
function maxSumAfterPartitioning(arr, k) {
  const n = arr.length;
  const dp = new Array(n + 1).fill(0);
  for (let i = 1; i <= n; i++) {
    let best = 0, curMax = 0;
    for (let j = 1; j <= Math.min(k, i); j++) {
      curMax = Math.max(curMax, arr[i - j]);
      best = Math.max(best, dp[i - j] + curMax * j);
    }
    dp[i] = best;
  }
  return dp[n];
}
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 best = 0, curMax = 0;
            for (int j = 1; j <= Math.min(k, i); j++) {
                curMax = Math.max(curMax, arr[i - j]);
                best = Math.max(best, dp[i - j] + curMax * j);
            }
            dp[i] = best;
        }
        return dp[n];
    }
}
int maxSumAfterPartitioning(vector<int>& arr, int k) {
    int n = arr.size();
    vector<int> dp(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        int best = 0, curMax = 0;
        for (int j = 1; j <= min(k, i); j++) {
            curMax = max(curMax, arr[i - j]);
            best = max(best, dp[i - j] + curMax * j);
        }
        dp[i] = best;
    }
    return dp[n];
}
Time: O(n·k) Space: O(n)