123. Best Time to Buy and Sell Stock III(买卖股票的最佳时机 III)
题目大意
You are given an array prices
where prices[i]
is the price of a given stock on the i
th day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
中文释义
给定一个数组 prices
,其中 prices[i]
是第 i
天给定股票的价格。
找出你能获得的最大利润。你最多可以完成两笔交易。
注意:你不能同时进行多笔交易(即,你必须在再次购买前出售股票)。
示例
- 示例 1:
- 输入:
prices = [3,3,5,0,0,3,1,4]
- 输出:
6
- 解释:第 4 天买入(价格 = 0)并在第 6 天卖出(价格 = 3),利润 = 3-0 = 3。
然后在第 7 天买入(价格 = 1)并在第 8 天卖出(价格 = 4),利润 = 4-1 = 3。
- 输入:
- 示例 2:
- 输