leetcode + 买卖股票,DP,一次扫描

点击打开链接
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() <=1) return 0;
        int low = prices[0], maxProfit =0;
        for(int i=1; i<prices.size(); i++){
            int profit = prices[i] - low;
            if(maxProfit < profit) maxProfit = profit;
            if(prices[i] < low) low = prices[i];
        }
        return maxProfit;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/80648802