forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst Bad Version.js
40 lines (35 loc) · 894 Bytes
/
First Bad Version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Runtime: 109 ms (Top 17.20%) | Memory: 41.9 MB (Top 54.94%)
/**
* 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) {
let ceiling = n
let floor = 1
let firstBadVersion = -1
while (floor <= ceiling) {
const middle = Math.floor((ceiling + floor) / 2)
if (isBadVersion(middle)) {
firstBadVersion = middle
ceiling = middle - 1
} else {
floor = middle + 1
}
}
return firstBadVersion
};
};