6-line C++ solution


  • 0
    G
        int maxProfit(vector<int>& prices) {
            int min = INT32_MAX, profit = 0;
            for (int i = 0; i < prices.size(); i++) {
                if (prices[i] <= min) min = prices[i];
                else profit = max(profit, prices[i] - min);
            }
            return profit;
        }
    

Log in to reply
 

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