컴퓨터공학/알고리즘 문제 풀이

[LeetCode] 278. First Bad Version

TaeGyeong Lee 2023. 2. 13. 17:46

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

피봇을 사용하여 탐색하는 문제입니다.

 

First Bad Version - LeetCode

Can you solve this real interview question? First Bad Version - You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed base

leetcode.com


솔루션

# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:

class Solution:
    def firstBadVersion(self, n: int) -> int:
        pivot = n//2
        pivot_smallest = 1
        pivot_largest = n

        while pivot_smallest != pivot_largest:
            if isBadVersion(pivot) == False:
                pivot_smallest = pivot + 1
                pivot = (pivot_largest + pivot_smallest)//2
            else:
                pivot_largest = pivot
                pivot = (pivot_largest + pivot_smallest)//2

        return pivot_smallest