Created
May 1, 2020 23:44
-
-
Save OmarKhattab/2d19983f64cd07472cda8e7d8a55a61a to your computer and use it in GitHub Desktop.
Leet Code First Bad Version Javascript Solution
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
/** | |
* 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