We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1140238 commit d9a4fc9Copy full SHA for d9a4fc9
1 file changed
best-time-to-buy-and-sell-stock/sadie100.ts
@@ -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