Tuesday, August 29, 2017

121. Best Time to Buy and Sell Stock

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
Solution
    int maxProfit(vector<int>& prices) {
        if(prices.size()<2) return 0;
        int b = prices[0], p = 0;
        for(int i=1; i<prices.size();i++) {
            p = max(prices[i] - b, p);
            if(prices[i]<b) b = prices[i];
        }
        return p;
    }

No comments:

Post a Comment