Skip to content

Commit 9151908

Browse files
committed
feat(soobing): best-time-to-buy-and-sell-stock
1 parent 742008b commit 9151908

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
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)