Skip to content

Instantly share code, notes, and snippets.

@kylebakerio
Last active July 7, 2016 15:17
Show Gist options
  • Save kylebakerio/cdb413c4dc747729efc50c56b4ad0741 to your computer and use it in GitHub Desktop.
Save kylebakerio/cdb413c4dc747729efc50c56b4ad0741 to your computer and use it in GitHub Desktop.
function inAndOut(prices) {
let high = prices[0];
let low = prices[0];
let lowIndex = 0;
let bestGap = 0;
const bestIndexes = [];
const currentGap = () => high - low;
for (var i = 1; i < prices.length; i++) {
if (prices[i] < low) {
// new slope; set new low, reset high.
low = prices[i];
lowIndex = i;
high = prices[i];
}
else if (prices[i] > high) {
// new peak on current slope
high = prices[i];
if (currentGap() > bestGap) {
// new best
bestGap = currentGap();
bestIndexes[0] = lowIndex;
bestIndexes[1] = i;
}
}
}
return bestIndexes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment