1. 程式人生 > >leetcode + 買賣股票,DP,一次掃描

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;
    }
};