본문으로 바로가기

LeetCode - Best Time to Buy and Sell Stock II

category Algorithm/LeetCode 2022. 1. 15. 23:19

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

 

Best Time to Buy and Sell Stock II - 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

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    let max = 0;
    for(let i= 0 ; i <prices.length-1; i++){
        if(prices[i]<prices[i+1]){
            max += (prices[i+1]-prices[i])
        }
    }
    return max;
};

너무 간단해서 뭔가 놓친 것이 있나 고민했던 문제.

 

1. 다음 날의 가격이 높은 경우에 더해주면 된다....

'Algorithm > LeetCode' 카테고리의 다른 글

LeetCode - Two Sum II - Input Array Is Sorted  (0) 2022.01.16
LeetCode - Two Sum  (0) 2022.01.16
LeetCode - Add Two Numbers  (0) 2022.01.16
LeetCode - Triangle  (0) 2022.01.15
LeetCode - Jump Game II  (0) 2022.01.15