- 입력
- 인자 1: arr
- number 타입을 요소로 갖는 배열
- arr[i] 는 정수
- 출력
- number 타입을 리턴 해야 한다.
자신이 구매한 날 이후의 날짜에 그 날짜에 해당하는 주식의 가격으로 팔 수 있으며 그 값을 최대가 되게 하고 최대가되는 그 값을 반환하라.
var maxProfit = function(prices) {
const dy = Array.from({length:prices.length}, ()=>0)
for(let i = 0 ; i < prices.length; i++){
for(let j = i+1; j<prices.length; j++){
if(dy[i] < prices[j]-prices[i]){
dy[i] = prices[j]-prices[i]
}
}
}
const answer = Math.max(...dy)
return answer
};
var maxProfit = function(prices) {
const dy = Array.from({length:prices.length}, ()=>0)
if (prices===[]) return 0
dy[dy.length-1] = prices[prices.length-1]
let result = Number.MIN_SAFE_INTEGER
for(let i = prices.length-2; i>=0; i--){
dy[i] = Math.max(dy[i+1], prices[i])
}
for(let j= 0 ; j<prices.length; j++){
result = Math.max(result, dy[j]-prices[j])
}
return result
}