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 92359ca commit d35c23fCopy full SHA for d35c23f
best-time-to-buy-and-sell-stock/HC-kang.ts
@@ -0,0 +1,19 @@
1
+// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2
+// T.C: O(N)
3
+// S.C: O(1)
4
+function maxProfit(prices: number[]): number {
5
+ let min = prices[0];
6
+ let max = prices[0];
7
+ let candidate = prices[0];
8
+
9
+ for (let i = 1; i < prices.length; i++) {
10
+ if (prices[i] < candidate) {
11
+ candidate = prices[i];
12
+ } else if (prices[i] - candidate > max - min) {
13
+ min = candidate;
14
+ max = prices[i];
15
+ }
16
17
18
+ return max - min;
19
+}
0 commit comments