-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstBadVersion.js
More file actions
40 lines (22 loc) · 841 Bytes
/
Copy pathFirstBadVersion.js
File metadata and controls
40 lines (22 loc) · 841 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
// https://leetcode.com/problems/first-bad-version/
let isBadVersion = function(bool){
return bool
}
var solution = function(isBadVersion) {
let arr = [false,false,false,true,true,true, true];
let n = arr.length;
return function(n) {
//initializes the boundaries
let left = 1,right = n;
while(left <= right){
// set the middle index
let pivot = left + Math.floor((right - left) / 2 );
// if version is bad search in the left half of the array
if(isBadVersion(pivot)) right = pivot;
// if not search on the right
else left = pivot + 1;
}
return left;
};
};
console.log(solution(isBadVersion,arr));