class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算最大收益
* @param prices int整型vector 股票每一天的价格
* @return int整型
*/
//只要涨价,我就先买后卖,将差价加入结果中。
//只要降价,就按兵不动,更新当前位置。
int maxProfit(vector<int>& prices) {
int res = 0;
int cur = prices[0];
for(int i=1; i<prices.size(); i++){
if(prices[i] > cur){
res += prices[i] - cur;
cur = prices[i];
} else{
cur = prices[i];
}
}
return res;
// write code here
}
};

京公网安备 11010502036488号