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 050b47f commit 30c323fCopy full SHA for 30c323f
โbest-time-to-buy-and-sell-stock/jiji-hoon96.tsโ
@@ -0,0 +1,23 @@
1
+function maxProfit(prices: number[]): number {
2
+ if (prices.length <= 1) return 0;
3
+
4
+ let minPrice = prices[0];
5
+ let maxProfit = 0;
6
7
+ for (let i = 1; i < prices.length; i++) {
8
+ // ํ์ฌ ๊ฐ๊ฒฉ์ด ์ต์๊ฐ๋ณด๋ค ๋ฎ์ผ๋ฉด ์ต์๊ฐ ์ ๋ฐ์ดํธ
9
+ if (prices[i] < minPrice) {
10
+ minPrice = prices[i];
11
+ }
12
+ // ํ์ฌ ๊ฐ๊ฒฉ์ผ๋ก ํ์์ ๋์ ์ด์ต ๊ณ์ฐ
13
+ else {
14
+ const currentProfit = prices[i] - minPrice;
15
+ // ์ต๋ ์ด์ต ์ ๋ฐ์ดํธ
16
+ if (currentProfit > maxProfit) {
17
+ maxProfit = currentProfit;
18
19
20
21
22
+ return maxProfit;
23
+}
0 commit comments