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 65c57e6 commit 16009a3Copy full SHA for 16009a3
best-time-to-buy-and-sell-stock/soobing.ts
@@ -0,0 +1,14 @@
1
+function maxProfit(prices: number[]): number {
2
+ let minPrice = Infinity;
3
+ let maxProfit = 0;
4
+ for (let i = 0; i < prices.length; i++) {
5
+ if (prices[i] < minPrice) {
6
+ minPrice = prices[i];
7
+ }
8
+
9
+ if (prices[i] - minPrice > maxProfit) {
10
+ maxProfit = prices[i] - minPrice;
11
12
13
+ return maxProfit;
14
+}
0 commit comments