Skip to content

Instantly share code, notes, and snippets.

@treyhuffine
Last active July 6, 2020 13:54
Show Gist options
  • Save treyhuffine/4c4550e9de0017eb7253691fe66bc8c7 to your computer and use it in GitHub Desktop.
Save treyhuffine/4c4550e9de0017eb7253691fe66bc8c7 to your computer and use it in GitHub Desktop.
const getMaxProfit = (prices) => {
let maxProfit = 0;
for (let buyDay = 0; buyDay < prices.length; buyDay++) {
const buyPrice = prices[buyDay];
for (let sellDay = buyDay + 1; sellDay < prices.length; sellDay++) {
const sellPrice = prices[sellDay];
const currentProfit = sellPrice - buyPrice;
maxProfit = Math.max(maxProfit, currentProfit);
}
}
return maxProfit;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment