# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
start_index = 1
end_index = n
while start_index <= end_index:
if start_index == end_index:
if isBadVersion(start_index):
return start_index
return None
if end_index - start_index < 100:
for i in range(start_index, end_index+1):
if isBadVersion(i):
return i
mid = max(0, (end_index - start_index)//2) + start_index
if isBadVersion(mid):
end_index = mid
else:
start_index = mid