leetcode练习题 best-time-to-buy-and-sell-stock

解题思路

做了best-time-to-buy-and-sell-stock iii后,这一题算是其简化版。

代码

#include<algorithm>
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int buy1 = INT_MIN,sell1 = 0;
        for(int i = 0;i < prices.size();i++){
            buy1 = max(buy1,-prices[i]);
            sell1 = max(sell1,buy1 + prices[i]);
        }
        return sell1;
    }
};
发布了18 篇原创文章 · 获赞 0 · 访问量 51

猜你喜欢

转载自blog.csdn.net/qq_38303368/article/details/104983274