Skip to content

Commit af8473f

Browse files
committed
best-time-to-buy-and-sell-stock 풀이 추가
1 parent abea895 commit af8473f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(N)
2+
// SC: O(1)
3+
function maxProfit(prices: number[]): number {
4+
let maxProfit = 0
5+
let minPrice = Infinity
6+
7+
for(const price of prices) {
8+
maxProfit = Math.max(price - minPrice, maxProfit)
9+
minPrice = Math.min(price, minPrice)
10+
}
11+
12+
return maxProfit
13+
14+
};
15+

0 commit comments

Comments
 (0)