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 7ae7503 commit f5afe89Copy full SHA for f5afe89
best-time-to-buy-and-sell-stock/anniemon.js
@@ -0,0 +1,18 @@
1
+/**
2
+ * 시간 복잡도: prices.length만큼 순회하므로 O(n)
3
+ * 공간 복잡도: 상수 크기의 변수만 사용하므로 O(1)
4
+ */
5
6
+ * @param {number[]} prices
7
+ * @return {number}
8
9
+var maxProfit = function (prices) {
10
+ let maxProfit = 0;
11
+ let min = prices[0];
12
+
13
+ for (let i = 0; i < prices.length; i++) {
14
+ maxProfit = Math.max(maxProfit, prices[i] - min);
15
+ min = Math.min(min, prices[i]);
16
+ }
17
+ return maxProfit;
18
+};
0 commit comments