-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstBadVersion.js
More file actions
40 lines (39 loc) · 927 Bytes
/
firstBadVersion.js
File metadata and controls
40 lines (39 loc) · 927 Bytes
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
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
/**
* @param {function} isBadVersion()
* @return {function}
*/
const solution = (isBadVersion) => {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let left = 1
let right = n
const map = new Map()
while (left <= right) {
const mid = Math.floor((right + left) / 2)
map.set(mid, isBadVersion(mid))
if (mid === right && map.get(mid)) return mid
if (map.get(mid) && map.has(mid - 1) && map.get(mid - 1) == false) {
return mid
}
if (map.get(mid)) {
right = mid
}
if (!map.get(mid)) {
left = mid + 1
}
}
return index
};
};