题目

分析
感觉这题方法像道贪心啊。就是如果股票后面要跌就把前面挣的钱落袋为安。。。
代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
int cur_min = INT_MAX, cur_profit = 0, res = 0;
for (auto x : prices){
if (x - cur_min > cur_profit){
cur_profit = x - cur_min;
}else{
res += cur_profit;
cur_profit = 0;
cur_min = x;
}
}
return res + cur_profit;
}
};
网友评论