Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions.
(股票收益)
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example:
1. 动态规划
结合前几次的买卖股票的问题,这次的区别在于交易的次数至多 k 次。如此一来,我们一定可以根据前交易 k-1 次的交易来推导出再增加一交易的最大收入,具体实现过程如下:
1 | class Solution: |