Link: https://leetcode.com/problems/partition-array-for-maximum-sum/
Solution:
Intuition
The trickiest thing about this problem in realizing that there is no greedy solution. The constraints hint at this as well. Max in subarray brings monotonic stack to mind, but there is no way to decide how long the subarray should be in the optimal case.
An optimized DP is the only solution because each index has an optimal solution, and we can cache that. Constructing the DP is a little bit tricky but it comes down to iterating from 0 to K and keeping track of the maximum value and the current sum (which is the maximum value multiplied by whatever iteration we are on). The code is pretty self explanatory.
Implementation
Visual
Review 1
Great problem. Solved it pretty fast. The key is realizing that this is a subsequence problem. Why? Because all the different contiguous splits are just contiguous subsequences!