Skip to content

Commit 2b976cd

Browse files
committed
add solution: best-time-to-buy-and-sell-stock
1 parent 479a691 commit 2b976cd

File tree

1 file changed

+15
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)