[LeetCode]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.

题目大意:

你是一名产品经理,正在领导团队开发一个新产品。不幸的是,产品的最新版本没有通过质量检测。由于每一个版本都是基于上一个版本开发的,某一个损坏的版本之后的所有版本全都是坏的。

假设有n个版本[1, 2, ..., n],你想要找出第一个损坏的版本,它导致所有后面的版本都坏掉了。

给你一个API bool isBadVersion(version),返回某一个版本是否损坏。实现一个函数找出第一个损坏的版本。你应该最小化调用API的次数。

解题思路:

二分法(Binary Search),详见代码。

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
        """
        left, right = 1, n
        while left <= right:
            mid = (left + right) / 2
            if isBadVersion(mid):
                right = mid - 1
            else:
                left = mid + 1
        return left

 

本文链接:http://bookshadow.com/weblog/2015/09/07/leetcode-first-bad-version/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

评论
  1. traceformula.blogspot.com traceformula.blogspot.com 发布于 2015年9月8日 14:37 #

    Another point of view:

    class Solution(object):
    def firstBadVersion(self, n):
    start, middle, end = 1, 1, n
    while start <= end:
    middle = (start + end) / 2
    if isBadVersion(middle): end = middle - 1
    else: start = middle + 1
    return end + 1

    URL: http://traceformula.blogspot.com/2015/09/first-bad-version-leetcode.html

张贴您的评论