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 12117a4 commit dc5bd6fCopy full SHA for dc5bd6f
best-time-to-buy-and-sell-stock/uraflower.js
@@ -0,0 +1,18 @@
1
+/**
2
+ * 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수
3
+ * @param {number[]} prices
4
+ * @return {number}
5
+ */
6
+const maxProfit = function(prices) {
7
+ let min = prices[0];
8
+ let profit = 0;
9
+
10
+ for (const price of prices) {
11
+ min = Math.min(min, price);
12
+ profit = Math.max(profit, price - min);
13
+ }
14
15
+ return profit;
16
+};
17
+// 시간복잡도: O(n)
18
+// 공간복잡도: O(1)
0 commit comments