소프트웨어에 대한 모든 것

LeetCode 풀기 - 278. First Bad Version 본문

알고리즘/LeetCode

LeetCode 풀기 - 278. First Bad Version

앤테바 2021. 11. 10. 07:31
반응형

278. First Bad Version

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

 

First Bad Version - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

바이너리 서치를 방법을 사용한다.

# 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 = 1        
        right = n + 1        
        while left < right:
            mid = (left + right) // 2
            if isBadVersion(mid):
                right = mid
            else:            
                left = mid + 1
        
        return left

 

반응형
Comments