[codeKata] maxProfit

EJ__OH·2021년 12월 12일
0

Q. prices는 배열이며, 각 요소는 매일의 주식 가격입니다. 만약 한 번만 거래할 수 있다면, 제일 큰 이익은 얼마일까요?

A.

const maxProfit = prices => {
  const arr = [];

  for(let i = 0; i < prices.length; i++){
    for(let j = 0; j <= i; j++){
      let result = prices[i] - prices[j]
      arr.push(result)
      }
    }
  return Math.max(...arr);
  };
profile
Junior FE Developer

0개의 댓글