278. 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 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.


class VersionControl:
    
    def isBadVersion(version):
        return version >= 5
        # Run unit tests to check whether verison `id` is a bad version
        # return true if unit tests passed else false.
        """
        @param: n: An integer
        @return: An integer which is the first bad version.
        """
class Solution:

    def findFirstBadVersion(n):
        # write your code here
        start = 1
        end = n
        while (start + 1) < end:
            mid = (end + start) // 2
            if VersionControl.isBadVersion(mid):
                end = mid
            else:
                start = mid
                   
        if VersionControl.isBadVersion(start):
            return start
        else:
            return end


    if __name__ == '__main__':
        print (findFirstBadVersion(30000000000))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,871評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,973評論 3 20
  • 無論是寫作,演講,還是談判,我們都是希望表達(dá)自己的獨特觀點,同時快速的轉(zhuǎn)變別人的觀點。而怎么讓人快速認(rèn)同我們的觀點...
    諸葛妙計閱讀 470評論 0 0
  • 上午8點半剛過,剛進(jìn)入工作狀態(tài),停電了。 第一反應(yīng),尼瑪又是公司里面電路漏電,樓層的漏電保護(hù)跳閘了,隔壁公司又要跳...
    謝鎮(zhèn)的動物園閱讀 560評論 0 1
  • 快樂是什么,最近總想這個問題。 心想事成,大家經(jīng)常在賀卡上這么寫,大約這是人們潛意識里認(rèn)可的一個快樂標(biāo)準(zhǔn),可細(xì)究起...
    雙寶飯閱讀 401評論 0 0

友情鏈接更多精彩內(nèi)容