Overview
This is a classic Front Partitioning problem. We want to find the optimal way to group the last `j` elements into a single partition, where `j` is at most `k`.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Partition an integer array into contiguous subarrays of length at most k. After partitioning, each element is changed to become the maximum value of that subarray.
This is a classic Front Partitioning problem. We want to find the optimal way to group the last `j` elements into a single partition, where `j` is at most `k`.
`dp[i]` represents the maximum sum achievable for the prefix of length `i`. It answers: "What's the best I can do using the first `i` items?"
To compute `dp[i]`, we look BACKWARDS by `j` steps (up to `k`). We assume the items from `i-j` to `i-1` form a new partition. Their contribution is `max_val * j`.
The total score if we partition the last `j` items is: `dp[i-j] + (max_in_sub * j)`. We take the maximum of this over all valid `j`.