Problem statement

https://leetcode.com/problems/first-bad-version/

Solution

Just do binary search in O(log n) complexity

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def firstBadVersion(self, n):
        beg, end = 1, n
        while beg < end:
            mid = (beg + end)//2
            if isBadVersion(mid):
                end = mid
            else:
                beg = mid + 1
        return beg