first-bad-version
This commit is contained in:
parent
e95b6e4f96
commit
51695319c2
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue