Skip to content

Commit da5a7d5

Browse files
committed
feat: best-time-to-buy-and-sell-stock solution
1 parent d52bb73 commit da5a7d5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 최대 수익을 구하는 알고리즘
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(n)
5+
* - 공간 복잡도: O(1)
6+
* @param prices
7+
*/
8+
function maxProfit(prices: number[]): number {
9+
let min = prices[0]
10+
let total = 0
11+
12+
for(let i = 1 ; i < prices.length ; i++) {
13+
min = Math.min(min, prices[i])
14+
// console.log(dp[i],'===', dp[i-1], '===', prices[i])
15+
total = Math.max(total, prices[i] - min)
16+
}
17+
18+
return total
19+
}

0 commit comments

Comments
 (0)