Partition Array for Maximum Sum
Problem Statement:β
Given an integer arrayΒ arr, partition the array into (contiguous) subarrays of lengthΒ at mostΒ k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.
ReturnΒ the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in aΒ 32-bitΒ integer.
-
Example:
Example 1:
Input: arr = [1,15,7,9,2,5,10], k = 3
Output: 84
Explanation: arr becomes [15,15,15,9,10,10,10]
Example 2:
Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
Output: 83
Example 3:
Input: arr = [1], k = 1
Output: 1
β Solution: Memoizationβ
class Solution {
public:
// Recursive helper to compute max sum starting from index i
int solve(int i, int n, int k, vector<int> &arr, vector<int> &dp){
if(i == n) return 0;
if(dp[i] != -1) return dp[i];
int len = 0;
int maxi = INT_MIN;
int maxans = INT_MIN;
// Try all partitions of length up to k
for(int j = i; j < min(i + k, n); j++){
len++;
maxi = max(maxi, arr[j]); // Keep track of max in this partition
// Compute cost: max * len + solve remaining part
int ans = maxi * len + solve(j + 1, n, k, arr, dp);
maxans = max(maxans, ans); // Maximize result
}
return dp[i] = maxans;
}
int maxSumAfterPartitioning(vector<int>& arr, int k) {
int n = arr.size();
vector<int> dp(n + 1, -1); // dp[i] = max sum from index i to end
return solve(0, n, k, arr, dp);
}
};
β Solution: Tabulationβ
class Solution {
public:
int maxSumAfterPartitioning(vector<int>& arr, int k) {
int n = arr.size();
vector<int> dp(n + 1, 0); // dp[i] = max sum starting from i
// Fill dp array from end to start
for(int i = n - 1; i >= 0; i--){
int len = 0;
int maxi = INT_MIN;
int maxans = INT_MIN;
for(int j = i; j < min(i + k, n); j++){
len++;
maxi = max(maxi, arr[j]);
int ans = maxi * len + dp[j + 1];
maxans = max(ans, maxans);
}
dp[i] = maxans;
}
return dp[0];
}
};
π How It Worksβ
-
We are allowed to partition the array into blocks of size at most
k. -
For each block, we calculate its cost as:
max in block * size of block. -
We then recurse or build the DP table to compute the maximum sum possible after choosing all partitions optimally.
-
dp[i]stores the maximum sum we can achieve starting from indexi.
π§© Key Formula / Transitionβ
dp[i] = max(
max(arr[i..j]) * (j - i + 1) + dp[j + 1]
) for all j in [i, i+k-1]
β±οΈ Time & Space Complexityβ
| Approach | Time | Space |
|---|---|---|
| Memoization | O(N*K) | O(N) |
| Tabulation | O(N*K) | O(N) |
- We explore up to
kelements from each indexi.
β οΈ Edge Casesβ
k = 1β Only 1-element partitions.- All elements are the same β Partition into max
ksize to get max sum. k >= nβ Can take the whole array as a single partition.
π‘ Other Approachesβ
| Approach | Time | Comment |
|---|---|---|
| Recursive Only | Exponential β | Too slow |
| Memoization | O(N*K) β | Top-down |
| Tabulation | O(N*K) β | Bottom-up |
π Related Problemsβ
- Partition Array for Maximum Sum β Leetcode 1043
- Paint House Problem (GFG)
- Maximum Product Subarray
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.