-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
52 lines (42 loc) · 932 Bytes
/
solution.ts
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
49
50
51
52
/*
* @lc app=leetcode id=278 lang=javascript
*
* [278] First Bad Version
*/
// @lc code=start
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
type IsBadVersion = (n: number) => boolean;
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
type Checker = (n: number) => number;
type Solution = (isBadVersion: IsBadVersion) => Checker;
/**
* @param {function} isBadVersion()
* @return {function}
*/
const solution: Solution = (isBadVersion) => (n) => {
// * ['48 ms', '88.25 %', '33.7 MB', '76.92 %']
let left = 1;
let right = n;
while (left < right) {
let mid = ~~((right + left) / 2);
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return right;
};
// @lc code=end
export { solution };