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 2200f1d commit 34258faCopy full SHA for 34258fa
1 file changed
best-time-to-buy-and-sell-stock/hyejj19.ts
@@ -0,0 +1,26 @@
1
+function maxProfit(prices: number[]): number {
2
+ let minPrice = Number.MAX_SAFE_INTEGER;
3
+ let maxProfit = 0;
4
+
5
+ for (let p of prices) {
6
+ if (minPrice >= p) {
7
+ minPrice = p;
8
+ continue;
9
+ } else {
10
+ let profit = p - minPrice;
11
+ if (profit > maxProfit) maxProfit = profit;
12
+ }
13
14
+ return maxProfit;
15
+}
16
17
18
+ let minPrice = Infinity;
19
20
21
22
+ minPrice = Math.min(minPrice, p);
23
+ maxProfit = Math.max(maxProfit, p - minPrice);
24
25
26
0 commit comments