반응형
1. 문제
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
2. 코드
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let max = 0;
let maxProfit = 0;
for(let i=prices.length-1;i>=0;i--){
if(prices[i] > max){
let bigger = false;
for(let j=i-1;j>=0;j--){
if(prices[j] < prices[i]){
bigger = true;
break;
}
}
if(bigger) max = prices[i];
}else{
if(max - prices[i] > 0 && max - prices[i] > maxProfit)
maxProfit = max - prices[i];
}
}
return maxProfit;
};
반응형
'리트코드 > easy' 카테고리의 다른 글
[리트코드] 2806. Account Balance After Rounded Purchase - js (0) | 2023.09.05 |
---|---|
[리트코드] 205. Isomorphic Strings - js (0) | 2023.09.02 |
[리트코드] 20. Valid Parentheses - js (0) | 2023.08.23 |
[리트코드] 69. Sqrt(x) - js (0) | 2023.08.21 |
[리트코드] 509. Fibonacci Number - js (0) | 2023.08.20 |