返回介绍

First Bad Version

发布于 2025-02-22 13:01:24 字数 3357 浏览 0 评论 0 收藏 0

Source

Problem

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 isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code's annotation part.

Example

Given n = 5 :

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

Here we are 100% sure that the 4th version is the first bad version.

Note

Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion(v)

Challenge

You should call isBadVersion as few as possible.

题解

基础算法中 Binary Search 的 lower bound. 找出满足条件的下界即可。

Python

#class VersionControl:
#  @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 VersionControl.isBadVersion(10) to check whether version 10 is a
# bad version.
class Solution:
  """
  @param n: An integers.
  @return: An integer which is the first bad version.
  """
  def findFirstBadVersion(self, n):
    lb, ub = 0, n + 1
    while lb + 1 < ub:
      mid = lb + (ub - lb) / 2
      if VersionControl.isBadVersion(mid):
        ub = mid
      else:
        lb = mid

    return lb + 1

C++

/**
 * class VersionControl {
 *   public:
 *   static bool isBadVersion(int k);
 * }
 * you can use VersionControl::isBadVersion(k) to judge whether
 * the kth code version is bad or not.
*/
class Solution {
public:
  /**
   * @param n: An integers.
   * @return: An integer which is the first bad version.
   */
  int findFirstBadVersion(int n) {
    int lb = 0, ub = n + 1;
    while (lb + 1 < ub) {
      int mid = lb + (ub - lb) / 2;
      if (VersionControl::isBadVersion(mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

    return lb + 1;
  }
};

Java

/**
 * public class VersionControl {
 *   public static boolean isBadVersion(int k);
 * }
 * you can use VersionControl.isBadVersion(k) to judge whether
 * the kth code version is bad or not.
*/
class Solution {
  /**
   * @param n: An integers.
   * @return: An integer which is the first bad version.
   */
  public int findFirstBadVersion(int n) {
    int lb = 0, ub = n + 1;
    while (lb + 1 < ub) {
      int mid = lb + (ub - lb) / 2;
      if (VersionControl.isBadVersion(mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

    return lb + 1;
  }
}

源码分析

lower bound 的实现,这里稍微注意下 lb 初始化为 0,因为 n 从 1 开始。ub 和 lb 分别都在什么条件下更新就好了。另外这里并未考虑 n <= 0 的情况。

复杂度分析

二分搜索,O(logn)O(\log n)O(logn).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文