Thief of Wealth
article thumbnail

https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/564/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 

 

현재 날짜의 최대수익을 가지고 있으면된다!

 

/**
 * @param {number[]} prices
 * @return {number}
 */
const maxProfit = function (prices) {
  const i번째까지의최대수익 = Array.from({ length: prices.length }, () => 0);

  for (let i = 1; i < prices.length; i++) {
    i번째까지의최대수익[i] =
      i번째까지의최대수익[i - 1] +
      (prices[i] - prices[i - 1] >= 0 ? prices[i] - prices[i - 1] : 0);
  }

  return i번째까지의최대수익[i번째까지의최대수익.length - 1];
};

console.log(maxProfit([7, 1, 5, 3, 6, 4]));
console.log(maxProfit([1, 2, 3, 4, 5]));
console.log(maxProfit([7, 6, 4, 3, 1]));

 

profile on loading

Loading...