/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    let p1 = 0;
    let p2 = 1;
    let maxProfit =  0  ;
    while(p2<prices.length){
        if(prices[p2]<prices[p1]){
            p1 = p2;
        }else{
        maxProfit = Math.max(maxProfit, prices[p2]-prices[p1]);
        }
        p2++
    }
    return maxProfit
};
 

 
    
Top comments (0)