백엔드 공부에 치여도 오늘은 꼭 코드카타 문제를 풀고 넘어가야겠다는 생각으로 풀었닷.
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
}
};