[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version

[Lintcode]74. First Bad Version

[Leetcode]278. First Bad Version

  • 本题难度: [Lintcode]Medium/[Leetcode]
  • Topic: Binary Search

    Description

我的代码 for Leetcode(在Lintcode中因为isBadVersion调用太多次无法通过)

# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        if isBadVersion(1) is True:
            return 1
        else:
            if n==1:
                return 0
        l = 2
        r = n
        m = (l+r)//2
        while(l<=r):
            print(l,m,r)
            t1 = isBadVersion(m-1)
            t2 = isBadVersion(m)
            print(t1,t2)
            if t1 is False and t2 is True:
                print("ok")
                return m
            else:
                if t2 is False:
                    l = m+1
                else:
                    if t1 is True:
                        r = m-1
            m = (l+r)//2

别人的代码 for Lintcode

#class SVNRepo:
#    @classmethod
#    def isBadVersion(cls, id)
#        # Run unit tests to check whether verison `id` is a bad version
#        # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a
# bad version.
class Solution:
    """
    @param n: An integer
    @return: An integer which is the first bad version.
    """
    def findFirstBadVersion(self, n):
        # write your code here

        r = n-1
        l = 0
        while(l<=r):
            mid = l + (r-l)//2
            if SVNRepo.isBadVersion(mid)==False:
                l = mid+1
            else:
                r = mid-1
        return l

思路

大思路是二分法,但是我想岔了。

其实无需找到一个自身为true,前一个为false的id,只需要找到最后一个为false的id即可。这样可以减少一次比较。

  • 时间复杂度: O(log(n))

[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version

原文地址:https://www.cnblogs.com/siriusli/p/10357078.html

时间: 2024-10-08 22:26:37

[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version的相关文章

LeetCode 278 First Bad Version

LeetCode  278 First Bad Version // Forward declaration of isBadVersion API. bool isBadVersion(int version); int firstBadVersion(int n) { int start=1, end=n; while(start < end) { int mid=start+(end-start)/2; if(isBadVersion(mid)) end=mid; else start=m

LeetCode 278 First Bad Version(第一个坏版本)(二分法)(*)

翻译 你是一个产品经理,目前正在带领团队去开发一个新产品. 不幸的是,产品的上一个版本没有通过质量检测. 因为每个版本都是建立在前一个版本基础上开发的,所以坏版本之后的版本也都是坏的. 假设你有n个版本[1,2,...,n],并且你想找出造成后面所有版本都变坏的第一个坏版本. 给你一个API--bool isBadVersion(version),它能够确定一个版本是否是坏的. 实现一个函数去找出第一个坏版本. 你应该尽可能少地去调用这个API. 原文 You are a product man

Leetcode 278 First Bad Version 二分查找

题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int version); 3 4 class Solution { 5 public: 6 int firstBadVersion(int n) { 7 int l = 1, r = n , ans ; 8 while(l <= r){ 9 int m

LeetCode:First Bad Version - 第一个坏版本

1.题目名称 First Bad Version(第一个坏版本) 2.题目地址 https://leetcode.com/problems/first-bad-version/ 3.题目内容 英文: 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 c

lintcode 74 First Bad Version

The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version. You can call isBadVer

278. First Bad Version

https://leetcode.com/problems/first-bad-version/#/description 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

[LeetCode][JavaScript]First Bad Version

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 versi

LC.278. First Bad Version

https://leetcode.com/problems/first-bad-version/description/ 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

First Bad Version leetcode java

问题描述: 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