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 81d5e7d commit 6c7105cCopy full SHA for 6c7105c
best-time-to-buy-and-sell-stock/Lustellz.ts
@@ -0,0 +1,16 @@
1
+function maxProfit(prices: number[]): number {
2
+
3
+ let minPrice: number = prices[0];
4
+ let maxProfit: number = 0;
5
6
+ for (let i = 1; i < prices.length; i++) {
7
+ // 1. compare the number later than now
8
+ if (prices[i] < minPrice) {
9
+ // 2. if there's bigger number later, set it as the standard
10
+ minPrice = prices[i];
11
+ } else {
12
+ maxProfit = Math.max(maxProfit, prices[i] - minPrice);
13
+ }
14
15
+ return maxProfit;
16
+};
0 commit comments