Skip to content

Commit 9e39ef3

Browse files
authored
Create 121.BestTimetoBuyandSellStock.js
1 parent f482bf9 commit 9e39ef3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

121.BestTimetoBuyandSellStock.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function maxProfit(prices) {
2+
let minPrice = prices[0];
3+
let maxProfit = 0;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
if (prices[i] < minPrice) {
7+
minPrice = prices[i];
8+
} else
9+
{
10+
if(maxProfit < prices[i] - minPrice)
11+
{
12+
maxProfit = prices[i] - minPrice
13+
}
14+
}
15+
}
16+
17+
return maxProfit;
18+
}

0 commit comments

Comments
 (0)