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 5f06e53 + 891ad63 commit b6b287aCopy full SHA for b6b287a
โbest-time-to-buy-and-sell-stock/robinyoon-dev.jsโ
@@ -0,0 +1,22 @@
1
+/**
2
+ * @param {number[]} prices
3
+ * @return {number}
4
+ */
5
+var maxProfit = function(prices) {
6
+
7
+ // NOTE: ํด์ค ๋ณด๊ณ ์ด ์ฝ๋์ ๋๋ค.
8
+ // i ๋ฒ์งธ ๋ ์ prices[i]์ ๊ฐ๊ฒฉ์ผ๋ก ์ฃผ์์ ํ์์ ๊ฐ์ฅ ํฐ ์ด์ต์ ๋ด๋ ค๋ฉด ์ฃผ์์ ์ธ์ ์์ด์ผ ํ์๊น?
9
+ // ์ ๋ต์ ๋ฐ๋ก i ๋ฒ์งธ ๋ ์ด ์ค๊ธฐ ์ ์ ์ฃผ์์ด ๊ฐ์ฅ ์๋ ๋ ์ ๋๋ค!
10
11
+ let maxProfit = 0;
12
+ let minPrice = prices[0];
13
14
+ for(const price of prices){
15
+ const profit = price - minPrice
16
+ maxProfit = Math.max(maxProfit, profit);
17
+ minPrice = Math.min(price, minPrice);
18
+ }
19
20
+ return maxProfit;
21
22
+};
0 commit comments