Partition Array for Maximum Sum

Given an integer array arr, partition the array into contiguous subarrays of length at most k. After partitioning, each subarray has its 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 1
Inputarr = [1,15,7,9,2,5,10], k = 3
Output84
After partitioning, arr becomes [15, 15, 15, 9, 10, 10, 10], whose sum is 84.
Example 2
Inputarr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
Output83
The maximum possible sum after partitioning into subarrays of length at most 4 is 83.

Constraints

  • 1 <= arr.length <= 500
  • 0 <= arr[i] <= 10^9
  • 1 <= k <= arr.length

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate