Skip to content

Commit d9a4fc9

Browse files
committed
sadie100: add 1 solution
1 parent 1140238 commit d9a4fc9

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
prices를 순회하며 현재 최저값과의 차이를 구해서 최대 profit을 갱신한 뒤 최저값(구매가)를 갱신, 최종값을 리턴한다
3+
4+
시간복잡도 O(N) - N은 prices의 length
5+
*/
6+
7+
function maxProfit(prices: number[]): number {
8+
let buy
9+
let result = 0
10+
11+
for (let price of prices) {
12+
if (buy === undefined) {
13+
buy = price
14+
continue
15+
}
16+
result = Math.max(result, price - buy)
17+
buy = Math.min(price, buy)
18+
}
19+
20+
return result
21+
}

0 commit comments

Comments
 (0)