Code Kata_maxProfit

dabin *.◟(ˊᗨˋ)◞.*·2021년 9월 29일
0

CodeKata

목록 보기
9/9
post-thumbnail

백엔드 공부에 치여도 오늘은 꼭 코드카타 문제를 풀고 넘어가야겠다는 생각으로 풀었닷.

문제

prices는 배열이며, 각 요소는 매일의 주식 가격입니다. 
만약 한 번만 거래할 수 있다면 = 사고 팔 수 있다면, 제일 큰 이익은 얼마일까요?
  
Input: [7,1,5,3,6,4]
Output: 5

풀이

const maxProfit = prices => {
  let arr = [];
  
  for (let i = 0; i < prices.length-1; i++) {
    for (let j = i+1; j < prices.length; j++) {
      arr.push(prices[j] - prices[i]);
    } 
  }

  const Max = Math.max(...arr)

  if (Max > 0) {
    return Max
  } else {
    return 0
  }
};
profile
모르는것투성이

0개의 댓글