-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
First-Bad-Version.js
48 lines (42 loc) · 1 KB
/
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
41
42
43
44
45
46
47
48
/**
* First Bad Version
* Easy
*
* Find The First Bad Version
Example 1:
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
* Logic : Using binary search to find the first bad version.
if mid - 1 is not a bad version then mid is first bad version
if mid - 1 is a bad version then set end = mid - 1
if mid is not a bad version then set start = mid + 1
* Runtime: 68 ms
* Memory Usage: 36.3 MB
*
*/
var solution = function (isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function (n) {
let start = 0,
end = n;
while (start <= end) {
let mid = Math.floor((end + start) / 2);
if (isBadVersion(mid)) {
if (!isBadVersion(mid - 1)) {
return mid;
} else {
end = mid - 1;
}
} else {
start = mid + 1;
}
}
return -1;
};
};