Best Time to Buy and Sell Stock III
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 two 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. 动态规划
题中的意思是指可以进行至多两次买入卖出的操作。可以以 i 划分整个时间,根据Best Time to Buy and Sell Stock中最大只能买卖一次的算法计算[0,i]的最大收益以及[i,n]的最大收益。具体实现过程如下:
1 | class Solution: |