Skip to content

Commit

Permalink
Merge pull request #379 from IjayAbby/ft-dev
Browse files Browse the repository at this point in the history
Buy and sell stock
  • Loading branch information
AkashSingh3031 authored Oct 24, 2023
2 parents 70ec891 + 3ccb513 commit 76f6bfb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var maxProfit = function(prices) {
let minPrice = Number.MAX_VALUE;
let maxProfit = 0;
for(let i = 0; i < prices.length; i++) {
if(prices[i] < minPrice) {
minPrice = prices[i];
}
else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
};

console.log(maxProfit([7,1,5,3,6,4]));

0 comments on commit 76f6bfb

Please sign in to comment.