개발공부/algorithm

[leetcode][python] 278. First Bad Version - Binary Search

so.py 2021. 7. 4. 20:50

Problem

  • Leetcode 278: First Bad version
  • Level: Easy
  • Link
 

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

 

My Code

기본적인 이진탐색 문제다. 이진 탐색을 진행해주며 가장 먼저 발생하는 bad version을 체크해준다. 

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

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