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))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,391评论 3 20
  • 无论是写作,演讲,还是谈判,我们都是希望表达自己的独特观点,同时快速的转变别人的观点。而怎么让人快速认同我们的观点...
    诸葛妙计阅读 400评论 0 0
  • 上午8点半刚过,刚进入工作状态,停电了。 第一反应,尼玛又是公司里面电路漏电,楼层的漏电保护跳闸了,隔壁公司又要跳...
    谢镇的动物园阅读 527评论 0 1
  • 快乐是什么,最近总想这个问题。 心想事成,大家经常在贺卡上这么写,大约这是人们潜意识里认可的一个快乐标准,可细究起...
    双宝饭阅读 363评论 0 0