JavaScript solution with explanation


  • 0
    D
    /**
     * @param {number[]} prices
     * @return {number}
     */
     // you can buy only one stock
     // you can buy and sell same stock on same day... do this when you see that next day's stock is higher than today
    var maxProfit = function(prices) {
        
        let profit = 0;
        for(let i = 0; i < prices.length; i++){
            if(prices[i+1] - prices[i] > 0){
                profit += prices[i+1] - prices[i];
            }
        }
        
        return profit;
    };
    

Log in to reply
 

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