Skip to content

Commit 83b7c4a

Browse files
committed
Best Time to buy and sell stock 😸
1 parent 176270a commit 83b7c4a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

best-time-to-buy-and-sell-stock.js

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

0 commit comments

Comments
 (0)