Title Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
When K≥prices.size ()/2 o'clock: the title is equivalent to the case of K infinity.
When K < Prices.size ()/2 o'clock:
Using dp[m][n+1] to represent the [0,n] interval, the M-sale operation, the maximum profit obtained.
Then this profit must be the maximum value of the following data:
- Dp[m-1][n+1], that is, in the [0,n] interval, the m-1 trading operation, the maximum profit obtained.
- Dp[m][n], that is, in the [0,n-1] interval, the M-sale operation, the maximum profit obtained.
or a
- Max (Dp[m-1][z] + (Prices[n]-prices[z]), that is, in the [0,z-1] interval, a m-1 trade operation, and the last buy at z time, the last time to sell at n time, the maximum profit obtained.
While the value of n is fixed for Each loop, the value of Z < n is constantly updated, so it is rewritten as Prices[n] + max (Dp[m-1][z]-prices[z]).
Leetcode #188 best time to Buy and Sell Stock IV