class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 两次交易所能获得的最大收益
* @param prices int整型vector 股票每一天的价格
* @return int整型
*/
int maxProfit(vector<int>& prices) {
// write code here
int len = prices.size();
if(len<1) return 0;
int b1 = prices[0];
int s1 = 0;
int b2 = prices[0];
int s2 = 0;
for(int i =1;i<len;i++){
b1 = min(b1,prices[i]);
s1 = max(s1,prices[i]-b1);
b2 = min(b2,prices[i]-s1);
s2 = max(s2,prices[i] - b2);
}
return s2;
}
};

京公网安备 11010502036488号