LeetCode | 0278. First Bad Versionの最初のエラーバージョン【Python】


LeetCode 0278. First Bad Versionの最初のエラーバージョン【Easy】【Python】【二分】
Problem
LeetCode
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 based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example:
Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version. 

に質問
スナップ
あなたはプロダクトマネージャで、現在チームを率いて新しい製品を開発しています.残念なことに、あなたの製品の最新バージョンは品質検査に合格していません.各バージョンは以前のバージョンに基づいて開発されているため、エラーのバージョン以降のすべてのバージョンは間違っています.
nバージョン[1, 2, ..., n]があるとしたら、その後のすべてのバージョンでエラーが発生した最初のエラーのバージョンを見つけたいと思います.bool isBadVersion(version)インタフェースを呼び出すことで、バージョン番号versionがユニットテストでエラーが発生したかどうかを判断できます.最初のエラーのバージョンを検索する関数を実装します.APIを呼び出す回数をできるだけ減らすべきです.
例:
   n = 5,   version = 4          。

   isBadVersion(3) -> false
   isBadVersion(5) -> true
   isBadVersion(4) -> true

  ,4          。 

構想
にぶんたんさく
       1   n,   low      1,high      n。

時間複雑度:O(logn)空間複雑度:O(1)
Pythonコード
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

class Solution(object):
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        low, high = 1, n  # 1-n
        while low <= high:
            mid = int((low + high) / 2)
            if isBadVersion(mid) == False:
                low = mid + 1
            else:
                high = mid - 1
        return low

コードアドレス
GitHubリンク