Last active
September 24, 2016 13:56
-
-
Save YanLobat/6ea19811e24f7912f713d55070fff8d6 to your computer and use it in GitHub Desktop.
FloodDepth solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(A) { | |
let start = A[0]; | |
let depth = 0; | |
let result = 0; | |
let min = 100000000; | |
for (let i = 1; i < A.length; i++) { | |
if (A[i] < start) { | |
if ((A[i] - min) > depth) { | |
depth = A[i] - min; | |
} | |
if (A[i] < min) { | |
min = A[i]; | |
} | |
} | |
else { | |
depth = start - min; | |
if (result < depth) { | |
result = depth; | |
} | |
depth = 0; | |
min = 100000000; | |
start = A[i]; | |
} | |
} | |
if ((!result) || (result < depth)) { | |
result = depth; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment