Skip to content

Commit 16009a3

Browse files
committed
feat(soobing): best-time-to-buy-and-sell-stock
1 parent 65c57e6 commit 16009a3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)