Partition Array for Maximum Sum (LeetCode 1043): Solution and Explanation
Cut an array into contiguous blocks of length at most k. Every block becomes copies of its own maximum. Maximise the resulting sum.
Intuition
Focus on the last block. It must end at the last element and has some length L between 1 and k. Whatever L is, that block adds L times its maximum, and the elements before it form a smaller version of the same problem.
So the best answer for the first i elements only depends on the best answers for shorter prefixes. That is a one-dimensional DP over prefixes.
Growing the last block leftward one element at a time lets you keep its maximum in a running variable, so each candidate length costs constant time.
Approach: Prefix DP over the last block
- Let
dp[i]be the largest sum for the firstielements, withdp[0] = 0. - For each
ifrom 1 ton, setcur_max = 0and trylength = 1..min(k, i). - Update
cur_max = max(cur_max, arr[i - length]): the block now coversarr[i - length .. i - 1]. - Update
dp[i] = max(dp[i], dp[i - length] + cur_max * length). - Return
dp[n].
Step-by-step example
arr = [1,15,7,9,2,5,10], k = 3
- dp[1] = 1 and dp[2] = 30: the block [1,15] becomes 15 + 15.
- dp[3] = 45: the block [1,15,7] becomes three 15s. Shorter last blocks give 30 + 7 = 37 or 1 + 30 = 31.
- dp[4] = 54: [9] alone on top of dp[3] = 45 beats raising [7,9] to 9s (30 + 18 = 48).
- dp[5] = 63, dp[6] = 72 and dp[7] = 84: the last block [2,5,10] becomes three 10s on top of dp[4] = 54.
- The answer is 84, from [1,15,7] [9] [2,5,10].
Code: Python, Java, C++
1class Solution:
2 def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int:
3 n = len(arr)
4 dp = [0] * (n + 1)
5 for i in range(1, n + 1):
6 cur_max = 0
7 for length in range(1, min(k, i) + 1):
8 cur_max = max(cur_max, arr[i - length])
9 dp[i] = max(dp[i], dp[i - length] + cur_max * length)
10 return dp[n]1class Solution {
2 public int maxSumAfterPartitioning(int[] arr, int k) {
3 int n = arr.length;
4 int[] dp = new int[n + 1];
5 for (int i = 1; i <= n; i++) {
6 int curMax = 0;
7 for (int len = 1; len <= Math.min(k, i); len++) {
8 curMax = Math.max(curMax, arr[i - len]);
9 dp[i] = Math.max(dp[i], dp[i - len] + curMax * len);
10 }
11 }
12 return dp[n];
13 }
14}1class Solution {
2public:
3 int maxSumAfterPartitioning(vector<int>& arr, int k) {
4 int n = arr.size();
5 vector<int> dp(n + 1, 0);
6 for (int i = 1; i <= n; i++) {
7 int curMax = 0;
8 for (int len = 1; len <= min(k, i); len++) {
9 curMax = max(curMax, arr[i - len]);
10 dp[i] = max(dp[i], dp[i - len] + curMax * len);
11 }
12 }
13 return dp[n];
14 }
15};Time and space complexity
O(n * k)
Each of the n positions tries at most k block lengths, each in constant time.
O(n)
One array of n + 1 values. Only the last k + 1 entries are ever read, so a ring buffer of size k + 1 also works.
Other approaches
Memoised recursion from the front (time O(n * k), space O(n))
Define best(start) as the answer for arr[start:], try every first block of length 1..k and cache. Same complexity, written top-down.
Brute force over cut points (time O(2^n), space O(n))
Try every way to place cuts. There are exponentially many partitions, so this only works for tiny arrays.
Common mistakes
- Recomputing the block maximum from scratch for each length makes the loop O(n * k^2). Keep a running maximum as the block grows leftward.
- Letting the block run past index 0: the length loop must stop at min(k, i).
- Greedily raising neighbours of the largest element is wrong. The best split can leave a big value alone, as dp[4] in the example shows.
Frequently asked questions
Why does the DP look at the last block instead of the first?
Either works. Looking at the last block gives a forward loop where dp[i] reads only smaller indexes, which is easy to fill left to right.
Can the sum overflow?
The problem guarantees the answer fits in a 32-bit integer, and every intermediate dp value is at most the final answer's scale, so int is enough as stated.
Is there an O(n) solution?
Not in general. The standard solution is O(n * k), which is at most 250,000 steps for the given limits.
Is the best partition unique?
No, several splits can reach the same sum. The problem only asks for the sum, so any of them is fine.