My intuitive c++ solution


  • 0
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
        int cost = 2147483647; // max int
        int profit = 0;
        for (int i = 0;i<prices.size();++i)
        {
            int p = prices[i] - cost;
            if (p > profit)
                profit = p;
            if (prices[i] < cost)
                cost = prices[i];
        }
        return profit;
    }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.