Skip to content

Commit 1ce733b

Browse files
committed
best-time-to-buy-and-sell-stock
1 parent edddfe9 commit 1ce733b

File tree

1 file changed

+11
-0
lines changed

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+
var maxProfit = function (prices) {
2+
let minPrice = prices[0];
3+
let maxProfit = 0;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
minPrice = Math.min(minPrice, prices[i]);
7+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
8+
}
9+
10+
return maxProfit;
11+
};

0 commit comments

Comments
 (0)