LeetCode 123. 买卖股票的最佳时机 III
题目描述
给定一个数组,它的第 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 。
LeetCode 123. 买卖股票的最佳时机 III
提示:
1 <= prices.length <= 105
0 <= prices[i] <= 105
一、解题关键词
动态规划。状态转移
二、解题报告
1.思路分析
2.时间复杂度
3.代码示例
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int buy1 = -prices[0],sell1 = 0;
int buy2 = -prices[0],sell2 = 0;
for(int i = 1; i < len; i++){
buy1 = Math.max(buy1,-prices[i]);
sell1 = Math.max(sell1,buy1 + prices[i]);
buy2 = Math.max(buy2,sell1 - prices[i]);
sell2 = Math.max(sell2,buy2 + prices[i]);
}
return sell2;
}
}
2.知识点