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.
2 parents af404b7 + 8c2011e commit 49625e5Copy full SHA for 49625e5
best-time-to-buy-and-sell-stock/Lustellz.ts
@@ -0,0 +1,21 @@
1
+function maxProfit(prices: number[]): number {
2
+
3
+ // Runtime: 1ms
4
+ // Memory: 66.23MB
5
+ let minPrice: number = prices[0];
6
+ let maxProfit: number = 0;
7
8
+ for (let i = 1; i < prices.length; i++) {
9
+ // 1. compare the number later than now
10
+ if (prices[i] < minPrice) {
11
+ // 2. if there's bigger number later, set it as the standard
12
+ minPrice = prices[i];
13
+ } else {
14
+ maxProfit = Math.max(maxProfit, prices[i] - minPrice);
15
+ }
16
17
+ return maxProfit;
18
19
+ // What I was planning to do: Recursion
20
+ // Result: referring other's code
21
+};
0 commit comments