Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 608 Bytes

File metadata and controls

39 lines (29 loc) · 608 Bytes

278 First Bad Version

Description

link


Solution

  • Binary Search

Code

O(logn)

# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        l, r = 1, n
        while l < r:
            m = (l + r) // 2
            if isBadVersion(m):
                r = m
            else:
                l = m + 1
        return l