Partition Array for Maximum Sum (LeetCode 1043): Solution and Explanation

Medium
Array
Dynamic Programming

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

  1. Let dp[i] be the largest sum for the first i elements, with dp[0] = 0.
  2. For each i from 1 to n, set cur_max = 0 and try length = 1..min(k, i).
  3. Update cur_max = max(cur_max, arr[i - length]): the block now covers arr[i - length .. i - 1].
  4. Update dp[i] = max(dp[i], dp[i - length] + cur_max * length).
  5. Return dp[n].

Step-by-step example

arr = [1,15,7,9,2,5,10], k = 3
  1. dp[1] = 1 and dp[2] = 30: the block [1,15] becomes 15 + 15.
  2. dp[3] = 45: the block [1,15,7] becomes three 15s. Shorter last blocks give 30 + 7 = 37 or 1 + 30 = 31.
  3. dp[4] = 54: [9] alone on top of dp[3] = 45 beats raising [7,9] to 9s (30 + 18 = 48).
  4. 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.
  5. 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]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n * k)

Each of the n positions tries at most k block lengths, each in constant time.

Space

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.