JavaScript solution with `reduce`


  • 0
    A
    function maxProfit(prices) {
        let min = Number.MAX_SAFE_INTEGER; 
    
        return prices.reduce((max, price) => {
            min = Math.min(min, price);
            
            return Math.max(max, price - min);
        }, 0);
    }
    

Log in to reply
 

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