Skip to content

Commit dc5bd6f

Browse files
authored
[ PS ] : Best Time to Buy and Sell Stock
1 parent 12117a4 commit dc5bd6f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수
3+
* @param {number[]} prices
4+
* @return {number}
5+
*/
6+
const maxProfit = function(prices) {
7+
let min = prices[0];
8+
let profit = 0;
9+
10+
for (const price of prices) {
11+
min = Math.min(min, price);
12+
profit = Math.max(profit, price - min);
13+
}
14+
15+
return profit;
16+
};
17+
// 시간복잡도: O(n)
18+
// 공간복잡도: O(1)

0 commit comments

Comments
 (0)