Partition Array for Maximum Sum
MediumDynamic Programming1d dp
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Split the integer array arr into contiguous pieces, each of length between 1 and k. Then replace every element of each piece with that piece's maximum value.
Return the largest possible sum of the array after this transformation.
Examples
Input: arr = [1,15,7,9,2,5,10], k = 3
Output: 84
Pieces `[1,15,7] [9] [2,5,10]` become `[15,15,15,9,10,10,10]`, which sums to 84.
Input: arr = [1], k = 1
Output: 1
The only piece is `[1]`.
Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
Output: 83
The best partition sums to 83.
Constraints
- `1 <= arr.length <= 500`
- `0 <= arr[i] <= 10^9`
- `1 <= k <= arr.length`
- The answer fits in a 32-bit signed integer
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.