278. First Bad Version
https://leetcode.com/problems/first-bad-version/
Python
Binary Search
class Solution:
def firstBadVersion(self, n: int) -> int:
l, r = 1, n
while l <= r:
cur = (l+r) // 2
if isBadVersion(cur):
r = cur - 1
else:
l = cur + 1
return l
Go
Binary Search
func firstBadVersion(n int) int {
left, right := 1, n
for left <= right {
pivot := (left+right) / 2
if isBadVersion(pivot) {
right = pivot - 1
} else {
left = pivot + 1
}
}
return left
}