First Bad Version

Question

  1. The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests.
  2. You can determine whether a version is bad by the following interface:
  3. Java:
  4. public VersionControl {
  5. boolean isBadVersion(int version);
  6. }
  7. C++:
  8. class VersionControl {
  9. public:
  10. bool isBadVersion(int version);
  11. };
  12. Python:
  13. class VersionControl:
  14. def isBadVersion(version)
  15. Find the first bad version.
  16. Note
  17. You should call isBadVersion as few as possible.
  18. Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion.
  19. Example
  20. Given n=5
  21. Call isBadVersion(3), get false
  22. Call isBadVersion(5), get true
  23. Call isBadVersion(4), get true
  24. return 4 is the first bad version
  25. Challenge
  26. Do not call isBadVersion exceed O(logn) times.

題 Search for a Range 的變形,找出左邊界即可。

Java

  1. /**
  2. * public class VersionControl {
  3. * public static boolean isBadVersion(int k);
  4. * }
  5. * you can use VersionControl.isBadVersion(k) to judge wether
  6. * the kth code version is bad or not.
  7. */
  8. class Solution {
  9. /**
  10. * @param n: An integers.
  11. * @return: An integer which is the first bad version.
  12. */
  13. public int findFirstBadVersion(int n) {
  14. // write your code here
  15. if (n == 0) {
  16. return -1;
  17. }
  18. int start = 1, end = n, mid;
  19. while (start + 1 < end) {
  20. mid = start + (end - start)/2;
  21. if (VersionControl.isBadVersion(mid) == false) {
  22. start = mid;
  23. } else {
  24. end = mid;
  25. }
  26. }
  27. if (VersionControl.isBadVersion(start) == true) {
  28. return start;
  29. } else if (VersionControl.isBadVersion(end) == true) {
  30. return end;
  31. } else {
  32. return -1; // not found
  33. }
  34. }
  35. }

C++

  1. /**
  2. * class VersionControl {
  3. * public:
  4. * static bool isBadVersion(int k);
  5. * }
  6. * you can use VersionControl::isBadVersion(k) to judge wether
  7. * the kth code version is bad or not.
  8. */
  9. class Solution {
  10. public:
  11. /**
  12. * @param n: An integers.
  13. * @return: An integer which is the first bad version.
  14. */
  15. int findFirstBadVersion(int n) {
  16. if (n < 1) {
  17. return -1;
  18. }
  19. int start = 1;
  20. int end = n;
  21. int mid;
  22. while (start + 1 < end) {
  23. mid = start + (end - start) / 2;
  24. if (VersionControl::isBadVersion(mid)) {
  25. end = mid;
  26. } else {
  27. start = mid;
  28. }
  29. }
  30. if (VersionControl::isBadVersion(start)) {
  31. return start;
  32. } else if (VersionControl::isBadVersion(end)) {
  33. return end;
  34. }
  35. return -1; // find no bad version
  36. }
  37. };

源碼分析

找左邊界和Search for a Range類似,但是最好要考慮到有可能end處也為good version,此部分異常也可放在開始的時候處理。

Python

  1. #class VersionControl:
  2. # @classmethod
  3. # def isBadVersion(cls, id)
  4. # # Run unit tests to check whether verison `id` is a bad version
  5. # # return true if unit tests passed else false.
  6. # You can use VersionControl.isBadVersion(10) to check whether version 10 is a
  7. # bad version.
  8. class Solution:
  9. """
  10. @param n: An integers.
  11. @return: An integer which is the first bad version.
  12. """
  13. def findFirstBadVersion(self, n):
  14. if n < 1:
  15. return -1
  16. start, end = 1, n
  17. while start + 1 < end:
  18. mid = start + (end - start) / 2
  19. if VersionControl.isBadVersion(mid):
  20. end = mid
  21. else:
  22. start = mid
  23. if VersionControl.isBadVersion(start):
  24. return start
  25. elif VersionControl.isBadVersion(end):
  26. return end
  27. return -1

Leetcode版題解

很明顯使用二分搜索,此題的測試只有Bad version必定出現的情況,會不會全部都是好的,可以向面試官詢問清楚,二分搜索的範圍,仍然使用下標範圍[0, n)控制邊界,要注意的是返回值是產品的編號,記得+1。另外直接使用產品編號[1, n+1)是行不通的,因為當n是INT_MAX時就會出現問題。

  1. // Forward declaration of isBadVersion API.
  2. bool isBadVersion(int version);
  3. class Solution {
  4. public:
  5. int firstBadVersion(int n) {
  6. if(isBadVersion(1)) return 1;
  7. int lo = 0, hi = n;
  8. while(lo < hi){
  9. int m = lo + (hi - lo)/2;
  10. if(!isBadVersion(m) and isBadVersion(m+1))
  11. return m+1;
  12. else if(isBadVersion(m) and isBadVersion(m+1))
  13. hi = m;
  14. else
  15. lo = m + 1;
  16. }
  17. }
  18. };