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.
2 parents 6411e48 + c973a58 commit f1b408dCopy full SHA for f1b408d
โbest-time-to-buy-and-sell-stock/seona926.js
@@ -0,0 +1,23 @@
1
+function maxProfit(prices) {
2
+ let minPrice = Infinity; // ์ต์ ๊ฐ๊ฒฉ์ ๋ฌดํ๋๋ก ์ด๊ธฐํ
3
+ let maxProfit = 0; // ์ต๋ ์ด์ต์ 0์ผ๋ก ์์
4
+
5
+ for (let i = 0; i < prices.length; i++) {
6
+ if (prices[i] < minPrice) {
7
+ // ๋ ์์ ๊ฐ๊ฒฉ์ ๋ฐ๊ฒฌํ๋ฉด ๊ทธ ๊ฐ๊ฒฉ์ ์ต์ ๊ฐ๊ฒฉ์ผ๋ก ์ ๋ฐ์ดํธ
8
+ minPrice = prices[i];
9
+ } else {
10
+ // ํ์ฌ ๊ฐ๊ฒฉ์์ ์ต์ ๊ฐ๊ฒฉ์ ๋บ ๊ฐ์ ๊ณ์ฐํ์ฌ ์ต๋ ์ด์ต์ ์ ๋ฐ์ดํธ
11
+ maxProfit = Math.max(maxProfit, prices[i] - minPrice);
12
+ }
13
14
15
+ return maxProfit;
16
+}
17
18
+/*
19
+ 1. ์๊ฐ๋ณต์ก๋: O(n)
20
+ - ๋ฐฐ์ด์ ํ ๋ฒ ์ํ
21
+ 2. ๊ณต๊ฐ๋ณต์ก๋: O(1)
22
+ - ์์ ๊ณต๊ฐ ์ฌ์ฉ
23
+*/
0 commit comments