Skip to content

Instantly share code, notes, and snippets.

@chaoyangnz
Last active February 25, 2019 09:10
Show Gist options
  • Save chaoyangnz/270b0b0013760a367ceec0e8bee9bc06 to your computer and use it in GitHub Desktop.
Save chaoyangnz/270b0b0013760a367ceec0e8bee9bc06 to your computer and use it in GitHub Desktop.
function solution(A) {
if(A.length < 3) return -1;
let depth = -1;
for(let p = 0; p < A.length; ++p) {
const q = findQ(A, p);
if(!q || q == A.length -1 || A[p] - A[q] <= depth) continue;
const r = findR(A, q);
if(r) {
const d = Math.min(A[p] - A[q], A[r] - A[q])
console.log('$$', p, q, r)
depth = Math.max(d, depth)
}
}
return depth;
}
function findQ(A, p) {
let q = undefined;
let i = p;
let j = i + 1;
while(i < A.length && j < A.length) {
if(A[j] < A[i]) {
i = j;
j++;
} else {
break; // found Q
}
}
if(i > p) q = i;
return q;
}
function findR(A, q) {
return A[q+1] > A[q] ? q + 1 : undefined
}
const d = solution([0, 1, 3, -2, 0, 1, 0, -3, 2, 3])
console.log('!!!', d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment