Given an integer array arr
, partition the array into (contiguous) subarrays of length at most k
. After partitioning, each subarray has their 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.
Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10]
Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83
Input: arr = [1], k = 1 Output: 1
1 <= arr.length <= 500
0 <= arr[i] <= 109
1 <= k <= arr.length
impl Solution {
pub fn max_sum_after_partitioning(arr: Vec<i32>, k: i32) -> i32 {
let mut dp = vec![0; arr.len() + 1];
for i in 0..dp.len() {
let mut max_val = 0;
for j in 0..(k as usize).min(dp.len() - i - 1) {
max_val = max_val.max(arr[i + j]);
dp[i + j + 1] = dp[i + j + 1].max(dp[i] + max_val * (j as i32 + 1));
}
}
*dp.last().unwrap()
}
}