Skip to content

Instantly share code, notes, and snippets.

@jearle
Created September 22, 2017 22:08
Show Gist options
  • Save jearle/170fd3efc8f0565dc7df964d64b3fb88 to your computer and use it in GitHub Desktop.
Save jearle/170fd3efc8f0565dc7df964d64b3fb88 to your computer and use it in GitHub Desktop.
const testArray = [5, -2, 3, 7, 3, 9, 3, 9, 9, 4, 4, 14, 13, 14, 20, 21, 11, 12];
// const testArray = [5, -2, 3, 6, 7, 8, 6];
// const testArray = [-5, 1, 1];
// const testArray = [1, 2, 3];
const solution = (a) => {
let cursorX = 0;
let cursorY = 0;
let cursorZ = 0;
let hasLowerValue = true;
while (cursorX < a.length) {
const valueX = a[cursorX];
const valueY = a[cursorY];
const valueZ = a[cursorZ];
if (valueX < valueZ) {
hasLowerValue = true;
}
if (valueX > valueY && hasLowerValue) {
cursorZ = cursorY;
cursorY = cursorX;
hasLowerValue = false;
}
cursorX++;
}
return cursorY;
};
console.log(solution(testArray));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment