https://leetcode.com/problems/first-bad-version/
피봇을 사용하여 탐색하는 문제입니다.
솔루션
# 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
'컴퓨터공학 & 정보통신 > 알고리즘 문제 풀이' 카테고리의 다른 글
[LeetCode] 1971. Find if Path Exists in Graph (0) | 2023.02.15 |
---|---|
[LeetCode] 989. Add to Array-Form of Integer (0) | 2023.02.15 |
[LeetCode] 205. Isomorphic Strings (0) | 2023.02.13 |
[LeetCode] 724. Find Pivot Index (0) | 2023.02.13 |
[LeetCode] 167. Two Sum II - Input Array Is Sorted (0) | 2023.02.12 |