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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
你是一个产品经理,目前正在带领一个组开发一个新产品。不幸的是,最近的产品版本质量检查失败了。因为每一个版本都是基于前一个版本开发的,所以一个坏版本后面的所有版本也都是坏的。假设有 n 个版本,你想找到第一个坏的版本。给了一个 API 可以检查版本是否是坏的,实施一个函数找到第一个坏的版本,要最小化调用 API 的次数。
解法1:暴力搜索Brute Force。 [Time Limit Exceeded]
解法2:二分法Binary Search。好版本和坏版本一定有个边界,用二分法来找这个边界,对mid值调用API函数,如果是坏版本,说明边界在左边,则把mid赋值给right,如果是好版本,则说明边界在右边,则把mid+1赋给left,最后返回left。
Java: Time: O(n), Space: O(1), [Time Limit Exceeded]
public int firstBadVersion(int n) { for (int i = 1; i < n; i++) { if (isBadVersion(i)) { return i; } } return n; }
Java: Time: O(logn), Space: O(1)
public int firstBadVersion(int n) { int left = 1; int right = n; while (left < right) { int mid = left + (right - left) / 2; if (isBadVersion(mid)) { right = mid; } else { left = mid + 1; } } return left; }
Python: wo
class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ left, right = 1, n while left < right: mid = left + (right - left) / 2 if isBadVersion(mid): right = mid else: left = mid + 1 return left
Python:
class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ left, right = 1, n while left <= right: mid = left + (right - left) / 2 if isBadVersion(mid): right = mid - 1 else: left = mid + 1 return left
C++: from 1 to n
// Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int left = 1, right = n; while (left < right) { int mid = left + (right - left) / 2; if (isBadVersion(mid)) right = mid; else left = mid + 1; } return left; } };
C++: from 0 to n - 1
// Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int left = 0, right = n - 1; while (left < right) { int mid = left + (right - left) / 2; if (isBadVersion(mid + 1)) right = mid; else left = mid + 1; } return right + 1; } };
类似题目:
[LeetCode] 35. Search Insert Position 搜索插入位置
[LeetCode] 165. Compare Version Numbers 比较版本数
[LeetCode] 374. Guess Number Higher or Lower 猜数字大小