Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OmarKhattab/2d19983f64cd07472cda8e7d8a55a61a to your computer and use it in GitHub Desktop.
Save OmarKhattab/2d19983f64cd07472cda8e7d8a55a61a to your computer and use it in GitHub Desktop.
Leet Code First Bad Version Javascript Solution
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
/**
* @param {function} isBadVersion()
* @return {function}
*/
var solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
return conq(0, n, isBadVersion)
};
};
const conq = (start, end, isBadVersion) => {
const mid = Math.floor((start + end) / 2);
if(isBadVersion(start)) return start;
if(isBadVersion(mid)) return conq(start + 1, mid, isBadVersion);
return conq(mid + 1, end, isBadVersion);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment