Simple Max/Min pointers (javascript)


  • 0
    J

    Been done a million times I'm sure, but here's my take.

    /**
     * @param {number[]} prices
     * @return {number}
     */
    var maxProfit = function(prices) {
        let maxProfit = 0;
        let minPrice = prices[0]
        
        for (let i = 0; i < prices.length; i++) {
            minPrice = Math.min(minPrice, prices[i]);
            
            let potential = prices[i] - minPrice;
            
            maxProfit = Math.max(maxProfit, potential)
        }
        
        return maxProfit;
    };

Log in to reply
 

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