LC123 Best Time to Buy and Sell
2020-08-26 本文已影响0人
Rookie118
本题链接:Best Time to Buy and Sell Stock III
本题标签:Array,Dynamic Programming
本题难度:
![](https://img.haomeiwen.com/i7019336/f5b67ea4314514ca.png)
![](https://img.haomeiwen.com/i7019336/a43ca7e3636793d3.png)
方案1:
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() < 2)
return 0;
int len = prices.size();
int min_buy1 = INT_MAX, min_buy2 = INT_MAX;
int max_sell1 = 0, max_sell2 = 0;
for(int i = 0; i < len; ++i)
{
min_buy1 = min(min_buy1, prices[i]);
max_sell1 = max(max_sell1, prices[i] - min_buy1);
min_buy2 = min(min_buy2, prices[i] - max_sell1);
max_sell2 = max(max_sell2, prices[i] - min_buy2);
}
return max_sell2;
}
};
时间复杂度:
空间复杂度: