Skip to content

Instantly share code, notes, and snippets.

@IGI-111
Created November 7, 2017 11:41
Show Gist options
  • Save IGI-111/d45c2ff6f77eaa6280274a6392c0ff1a to your computer and use it in GitHub Desktop.
Save IGI-111/d45c2ff6f77eaa6280274a6392c0ff1a to your computer and use it in GitHub Desktop.
function stockPicker(market) {
let lowest_index = 0;
const lowest_previous_index = market.map((x, i) => {
if (x < market[lowest_index]) {
lowest_index = i;
}
return lowest_index;
});
let best = {
high_index: 0,
low_index: 0,
profit: 0
};
market.forEach((x, i) => {
const current_profit = x - market[lowest_previous_index[i]];
if (current_profit > best.profit) {
best = {
high_index: i,
low_index: lowest_previous_index[i],
profit: current_profit,
};
}
});
return [best.low_index, best.high_index];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment