Skip to content

Commit 1696de2

Browse files
authored
Merge pull request #21 from argondev22/20251109/19-121-best-time-to-buy-and-sell-stock
Export maxProfit function for external use
2 parents 9d6b215 + 1a5c3a3 commit 1696de2

File tree

1 file changed

+11
-0
lines changed
  • src/leetcode/121_best-time-to-buy-and-sell-stock

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function maxProfit(prices: number[]): number {
2+
let minPrice = prices[0];
3+
let maxProfit = 0;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
7+
minPrice = Math.min(minPrice, prices[i]);
8+
}
9+
10+
return maxProfit;
11+
};

0 commit comments

Comments
 (0)