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 2624cbe commit 28c30e7Copy full SHA for 28c30e7
best-time-to-buy-and-sell-stock/sunjae95.js
@@ -0,0 +1,28 @@
1
+/**
2
+ * @description
3
+ * brainstorming:
4
+ * brute force
5
+ *
6
+ * time complexity: O(n)
7
+ * space complexity: O(1)
8
+ */
9
+var maxProfit = function (prices) {
10
+ let answer, min, max;
11
+ prices.forEach((price, i) => {
12
+ if (i === 0) {
13
+ min = price;
14
+ max = price;
15
+ answer = 0;
16
+ return;
17
+ }
18
+
19
+ if (price > max) max = price;
20
+ if (price < min) {
21
22
23
24
+ answer = Math.max(answer, max - min);
25
+ });
26
27
+ return answer;
28
+};
0 commit comments