- 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
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
/**
* 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
*/
let index = 0
return function(t) {
let middle = Math.ceil(t/2);
let lastMiddle = 20
/**
total 20
bad 17
lastM = 20
(20 - lastM(20)) /2 == 10 // first time, just 20/2 (cause (20 - 20) / 2 [ 0/2]!!)
(20 - lastM(10)) /2 === 15
(20 - lastM(15) ) / 2 === 13 // WRONG WAY
lastGood = 17
lastBad = 15
Pick middle: t / 2 === 10
isBad(10)
false:
lastGood = 10
isBad(15) // middle.= middle + middle/2 (m = 10 + 5 == 15)
false:
lastGood = 15
isBad(18) // m = (20 - 15) / 2 == 5 /2 == 3
true: // between 15 and 18 is first bad. set t to 18
lastBad = 18
isBad(17) // m = (18 - 15) /2 === 3/2 == 2
true:
lastBad = 17
isBad(16)
isBad(10):
false:
lastBad = 10
*/
};
};