9/20/2020

[LeetCode] 278. First Bad Version

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

Time complexity = O ( Log N )

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

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        
        left, right = 0, n
        
        while left <= right:
            mid = left + (right - left) // 2
            
            if not isBadVersion(mid):
                left = mid + 1
            else:
                right = mid - 1

        return left

No comments:

Post a Comment