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 8df437d commit c9e39dbCopy full SHA for c9e39db
โbest-time-to-buy-and-sell-stock/mmyeon.ts
@@ -0,0 +1,24 @@
1
+/**
2
+ *
3
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4
+ * - max profit์ ๊ตฌํ๋ ๋ฌธ์ ๋ก O(n)์ผ๋ก ํ๊ธฐ
5
+ * - ํ์ฌ ๊ฐ๊ฒฉ์์ ๊ฐ์ฅ ๋ฎ์ ๊ฐ๊ฒฉ์ ๋บ ๊ฐ์ max profit์ผ๋ก ์ค์
6
7
+ * ์๊ฐ๋ณต์ก๋ : O(n)
8
+ * - n์ prices ๊ธธ์ด, ์์ 1ํ ์ํํ๋๊น O(n)
9
10
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(1)
11
+ * - ๋ณ์ 2๊ฐ ์ฌ์ฉํ๋๊น O(1)
12
+ */
13
+
14
+function maxProfit(prices: number[]): number {
15
+ let minPrice = prices[0],
16
+ maxProfit = 0;
17
18
+ for (const price of prices) {
19
+ minPrice = Math.min(price, minPrice);
20
+ maxProfit = Math.max(maxProfit, price - minPrice);
21
+ }
22
23
+ return maxProfit;
24
+}
0 commit comments