Skip to content

Commit 4a5bc50

Browse files
committed
best time to buy and sell stock solution
1 parent 80d8f88 commit 4a5bc50

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function (prices) {
6+
// 가장 작은 수
7+
let minNum = prices[0];
8+
// 차이 값
9+
let maxProfit = 0;
10+
for (let i = 1; i < prices.length; i++) {
11+
minNum = Math.min(minNum, prices[i - 1]); // 이전꺼 중 가장 작은 수
12+
maxProfit = Math.max(maxProfit, prices[i] - minNum);
13+
}
14+
return maxProfit;
15+
};

0 commit comments

Comments
 (0)