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 390e210 commit cc061c9Copy full SHA for cc061c9
best-time-to-buy-and-sell-stock/gwbaik9717.js
@@ -0,0 +1,18 @@
1
+// Time complexity: O(n)
2
+// Space complexity: O(1)
3
+
4
+/**
5
+ * @param {number[]} prices
6
+ * @return {number}
7
+ */
8
+var maxProfit = function (prices) {
9
+ let answer = 0;
10
+ let minValue = Number.MAX_SAFE_INTEGER;
11
12
+ for (const price of prices) {
13
+ minValue = Math.min(minValue, price);
14
+ answer = Math.max(answer, price - minValue);
15
+ }
16
17
+ return answer;
18
+};
0 commit comments