5 line CPP solution


  • 21
    L
    int maxProfit(vector<int>& prices) {
        int maxPro = 0, minPrice = INT_MAX;
        for(int i = 0; i < prices.size(); i++) {
            minPrice = min(minPrice, prices[i]);
            maxPro = max(prices[i] - minPrice, maxPro);
        }
        return maxPro;
    }

Log in to reply
 

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