美文网首页
leetcode122 买卖股票的最佳时机II

leetcode122 买卖股票的最佳时机II

作者: 奥利奥蘸墨水 | 来源:发表于2019-12-30 00:59 被阅读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;
        }
    };
    

    相关文章

      网友评论

          本文标题:leetcode122 买卖股票的最佳时机II

          本文链接:https://www.haomeiwen.com/subject/wxzuoctx.html