leetcode#278 First Bad Version

정은경·2022년 6월 28일
0

알고리즘

목록 보기
112/125

1. 문제

2. 나의 문제

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

class Solution:
    def firstBadVersion(self, n: int) -> int:
        start_index = 1
        end_index = n
        
        while start_index <= end_index:
            if start_index == end_index:
                if isBadVersion(start_index):
                        return start_index
                return None
            if end_index - start_index < 100:
                for i in range(start_index, end_index+1):
                    if isBadVersion(i):
                        return i
            mid = max(0, (end_index - start_index)//2) + start_index
            if isBadVersion(mid):
                end_index = mid
            else:
                start_index = mid

Reference

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글