Javascript simple solution


  • 0
    C

    var maxProfit = function(prices) {
    if(prices.length==0){
    return 0;
    }
    let local = 0;
    let global = 0;
    for (let i=0;i<prices.length;i++){
    if(prices[i+1]==undefined){
    break;
    }
    local = Math.max(local+prices[i+1]-prices[i],0);
    global = Math.max(local,global);
    console.log(global)
    }
    return global;
    };


Log in to reply
 

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