1043. Partition Array for Maximum Sum
Asked at TikTok
Difficulty
Medium
Time
O(n * k)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given an integer array arr and an integer k. You must cut arr into contiguous pieces (subarrays), where each piece has length between 1 and k. Every element in a piece is replaced by the maximum value inside that piece. After all replacements, you add up the whole array.
Return the largest possible total you can get by choosing the best way to cut the array. Different cuts give different totals, so you must search for the best one.
A piece must be a block of neighbouring elements — you cannot skip elements or reorder them. Every element belongs to exactly one piece.
Cut into pieces, then copy each piece's max
Constraints
- ●
1 <= arr.length <= 500 - ●
0 <= arr[i] <= 10^9 - ●
1 <= k <= arr.length - ●
The answer fits in a 32-bit integer.
Examples
arr = [1,15,7,9,2,5,10], k = 384arr = [1,4,1,5,7,3,6,1,9,9,3], k = 483arr = [1], k = 11Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| TikTok | Medium | View all TikTok questions → |
How to Think About It
Greedy choices (always take pieces of length k, or always extend around big numbers) fail on small counterexamples. You need to try the options.
Key insight: let dp[i] be the best total for the prefix of length i. The last piece ends at index i - 1 and has some length len from 1 to k.
For each len, the piece contributes max(arr[i-len..i-1]) * len, so dp[i] = max over len of dp[i - len] + maxInPiece * len.
Compute the running maximum while extending len backward, so each state costs O(k).
Walkthrough for [1,15,7,9,2,5,10], k = 3: the best split is [1,15,7] [9] [2,5,10] -> 15*3 + 9 + 10*3 = 84.
Optimal Approach
Step 1: dp = [0] * (n + 1).
Step 2: For i from 1 to n:
cur = 0
For len from 1 to min(k, i):
cur = max(cur, arr[i - len])
dp[i] = max(dp[i], dp[i - len] + cur * len)
Step 3: Return dp[n].
Time: O(n * k). Space: O(n).
DP over prefixes
What Trips People Up in Real Interviews
Recomputing the piece maximum from scratch for each length — that adds another factor of k.
Off-by-one with 1-indexed dp versus 0-indexed arr. The last piece for dp[i] covers arr[i-len .. i-1].
Trying a greedy split. Always ask yourself for a counterexample before committing to greedy on partition problems.
Forgetting to cap len at i for the first few positions.
Solution Code
O(n * k) timedef maxSumAfterPartitioning(arr, k):
n = len(arr)
dp = [0] * (n + 1)
for i in range(1, n + 1):
cur = 0
for length in range(1, min(k, i) + 1):
cur = max(cur, arr[i - length])
dp[i] = max(dp[i], dp[i - length] + cur * length)
return dp[n]Frequently Asked Questions
What is the Partition Array for Maximum Sum problem?
Partition Array for Maximum Sum asks you to split an array into contiguous pieces of length at most `k`, replace every element of each piece with that piece's maximum, and return the largest possible total. It is a 1D DP where each state tries every possible length for the last piece.
How do you solve Partition Array for Maximum Sum?
The optimal approach runs in O(n * k) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Partition Array for Maximum Sum?
Partition Array for Maximum Sum is asked at TikTok. It is a medium difficulty problem.
What are common mistakes on Partition Array for Maximum Sum?
- Recomputing the piece maximum from scratch for each length — that adds another factor of `k`.
- Off-by-one with 1-indexed `dp` versus 0-indexed `arr`. The last piece for `dp[i]` covers `arr[i-len .. i-1]`.
- Trying a greedy split. Always ask yourself for a counterexample before committing to greedy on partition problems.
- Forgetting to cap `len` at `i` for the first few positions.