-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFirstBadVersion.kt
More file actions
65 lines (55 loc) · 1.96 KB
/
FirstBadVersion.kt
File metadata and controls
65 lines (55 loc) · 1.96 KB
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
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
abstract class VersionControl(open val badVersion: Int) {
fun isBadVersion(version: Int): Boolean {
return version >= badVersion
}
abstract fun firstBadVersion(n: Int): Int
}
/**
* Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one,
* which causes all the following ones to be bad.
*
* You are given an API bool isBadVersion(version) which returns whether version is bad.
* Implement a function to find the first bad version. You should minimize the number of calls to the API.
*
*
* Constraint `1 <= bad <= n <= 2^31 - 1`
*
* [Source](https://leetcode.com/problems/first-bad-version/)
*/
@UseCommentAsDocumentation
class Solution(override val badVersion: Int) : VersionControl(badVersion) {
override fun firstBadVersion(n: Int): Int {
// This is basically a binary search problem
return binarySearch(0, n)
}
private fun binarySearch(low: Int, high: Int): Int {
if (high < low) {
return -1
}
// HERE: high can go as far as (2^31 - 1) so need to use Long to accommodate
val mid = ((low.toLong() + high.toLong()) / 2).toInt()
val midElement = mid + 1
return if (isBadVersion(midElement)) {
if (mid == 0 || (mid > 0 && !isBadVersion(midElement - 1))) { // also check for previous element
// if previous element is not a bad version, then this is for sure the bad version
mid + 1
} else {
// previous element was not bad version so continue checking
binarySearch(low, mid - 1)
}
} else {
binarySearch(mid + 1, high)
}
}
}
fun main() {
run {
Solution(badVersion = 1702766719).firstBadVersion(n = 2126753390) shouldBe 1702766719
}
run {
Solution(badVersion = 4).firstBadVersion(n = 5) shouldBe 4
}
}