Last active
August 29, 2015 14:26
-
-
Save SeeThruHead/e60613535dab860e3eca to your computer and use it in GitHub Desktop.
This file contains 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 biggestGap(values) { | |
function diff(curr, before) { | |
return curr - Math.min.apply(null, before); | |
} | |
function diffs(values) { | |
return values.map(function(x, i, ar) { | |
return diff(x, ar.slice(0, i)); | |
}); | |
} | |
return Math.max.apply(null, diffs(values)); | |
} | |
// OR Thanks David Chambers | |
// max :: [a] -> a | |
const max = Function.apply.bind(Math.max, Math); | |
// biggestGap :: [Number] -> Number | |
const biggestGap = xs => max(xs.map((x, idx, xs) => max(xs.slice(idx)) - x)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finds the biggest gap between elements in an array. Where the smaller element must be in front of the larger.