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 c359aee commit e6d1631Copy full SHA for e6d1631
best-time-to-buy-and-sell-stock/sounmind.ts
@@ -0,0 +1,18 @@
1
+function maxProfit(prices: number[]): number {
2
+ let minPrice = Number.MAX_SAFE_INTEGER;
3
+ let maxProfit = 0;
4
+
5
+ for (let i = 0; i < prices.length; i++) {
6
+ if (prices[i] < minPrice) {
7
+ minPrice = prices[i];
8
+ }
9
10
+ const potentialProfit = prices[i] - minPrice;
11
12
+ if (maxProfit < potentialProfit) {
13
+ maxProfit = potentialProfit;
14
15
16
17
+ return maxProfit;
18
+}
0 commit comments