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 514f317Copy full SHA for 514f317
best-time-to-buy-and-sell-stock/gitsunmin.ts
@@ -0,0 +1,17 @@
1
+/**
2
+ * https://leetcode.com/problems/best-time-to-buy-and-sell-stock
3
+ * time complexity : O(n)
4
+ * space complexity : O(1)
5
+ */
6
+
7
+function maxProfit(prices: number[]): number {
8
+ let maxProfit = 0;
9
+ let [minPrice] = prices;
10
11
+ for (let price of prices) {
12
+ maxProfit = Math.max(maxProfit, price - minPrice);
13
+ minPrice = Math.min(minPrice, price);
14
+ }
15
16
+ return maxProfit;
17
+};
0 commit comments