first-bad-version

This commit is contained in:
Gleb Koval 2021-11-05 15:09:36 +00:00
parent e95b6e4f96
commit 51695319c2
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 19 additions and 0 deletions

19
first-bad-version/sol.go Normal file
View File

@ -0,0 +1,19 @@
package main
func isBadVersion(version int) bool
// Time: O(log(n))
// Space: O(1)
func firstBadVersion(n int) int {
move := 0
for n > 1 {
p := n / 2
if isBadVersion(move + p) {
n = p
} else {
n = n - p
move += p
}
}
return move + n
}