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 25b4c78 commit be77817Copy full SHA for be77817
best-time-to-buy-and-sell-stock/byol-han.js
@@ -0,0 +1,18 @@
1
+/**
2
+ * @param {number[]} prices
3
+ * @return {number}
4
+ */
5
+var maxProfit = function (prices) {
6
+ let minPrice = Infinity;
7
+ let maxProfit = 0;
8
+
9
+ for (let i = 0; i < prices.length; i++) {
10
+ if (prices[i] < minPrice) {
11
+ minPrice = prices[i]; // 지금까지 가장 싼 날
12
+ } else if (prices[i] - minPrice > maxProfit) {
13
+ maxProfit = prices[i] - minPrice; // 현재 이익이 최대 이익보다 클 때 maxProfit 갱신
14
+ }
15
16
17
+ return maxProfit;
18
+};
0 commit comments