Skip to content

Instantly share code, notes, and snippets.

@jebrial
Created October 18, 2017 11:15
Show Gist options
  • Save jebrial/ee432bd5aadae9fc4ccdef5f8fd31cdd to your computer and use it in GitHub Desktop.
Save jebrial/ee432bd5aadae9fc4ccdef5f8fd31cdd to your computer and use it in GitHub Desktop.
function getMaxProfit(stockPricesYesterday) {
let buy = stockPricesYesterday[0], sell = stockPricesYesterday[1];
for(let i = 1; i < stockPricesYesterday.length - 1; i++) {
if(stockPricesYesterday[i] < buy) {
buy = stockPricesYesterday[i];
}
if(stockPricesYesterday[i+1] > sell) {
sell = stockPricesYesterday[i+1]
}
}
return sell - buy;
}
console.log(getMaxProfit([10,11,5,8,11,9]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment