Skip to content

Instantly share code, notes, and snippets.

@aire-con-gas
Created December 12, 2017 20:34
Show Gist options
  • Select an option

  • Save aire-con-gas/04cb021537d0803352deee24f3df48ed to your computer and use it in GitHub Desktop.

Select an option

Save aire-con-gas/04cb021537d0803352deee24f3df48ed to your computer and use it in GitHub Desktop.
const maxProfit = (prices = []) => {
let minPrice = Number.MAX_SAFE_INTEGER;
let maxProfit = 0;
prices.forEach(price => {
if (price < minPrice) {
minPrice = price;
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
});
return maxProfit;
};
const prices = [7, 1, 5, 6, 3];
console.log('prices', prices);
console.log('maxProfit', maxProfit(prices));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment