N

(Leet Code JS)Best Time to Buy and Sell Stock 본문

Leet Code 알고리즘

(Leet Code JS)Best Time to Buy and Sell Stock

naeunchan 2022. 3. 8. 10:31
728x90
반응형

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

 

Best Time to Buy and Sell Stock - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

var maxProfit = function(prices) {
    const length = prices.length;
    let buy = prices[0];
    let profit = 0;
    
    for(let i = 1; i < length; i++){
        buy = Math.min(buy, prices[i]);
        profit = Math.max(profit, prices[i] - buy);
    }
    
    return profit;
};
728x90
반응형

'Leet Code 알고리즘' 카테고리의 다른 글

(Leet Code JS)Letter Case Permutation  (0) 2022.03.11
(Leet Code JS)Rotting Oranges  (0) 2022.03.10
(Leet Code JS)Permutation in String  (0) 2022.03.08
(Leet Code JS)Max Area of Island  (0) 2022.03.07
(Leet Code JS)Flood Fill  (0) 2022.03.07