leetcode122 买卖股票的最佳时机II

2019-12-30  本文已影响0人  奥利奥蘸墨水

题目

题目

分析

感觉这题方法像道贪心啊。就是如果股票后面要跌就把前面挣的钱落袋为安。。。

代码

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;
    }
};
上一篇下一篇

猜你喜欢

热点阅读