DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 121. Best Time to Buy and Sell Stock


/**
 * @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
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up