121. Best Time to Buy and Sell Stock

전민승·2023년 3월 7일
0
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
    let profit = 0;

    for (let i = 0; i < prices.length; i++) {
        for (let j = i + 1; j < prices.length - 1; j++) {
            if (prices[i] < prices[j]) {
                if(prices[j] - prices[i] > profit){
                    profit = prices[j] - prices[i]
                }
            }
        }
    }

    return profit
};
profile
Frontend Developer

0개의 댓글