Tuesday, September 8, 2015

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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Understand the problem:
A binary search problem. 

Code (Java):
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        if (n <= 0) {
            return 0;
        }
        
        int lo = 1;
        int hi = n;
        
        while (lo + 1 < hi) {
            int mid = lo + (hi - lo) / 2;
            
            if (isBadVersion(mid)) {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        
        if (isBadVersion(lo)) {
            return lo;
        }
        
        if (isBadVersion(hi)) {
            return hi;
        }
        
        return 0;
    }
}

Update on 10/24/15:
We use long instead of int to avoid the integer overflow problem. 

Code (Java):
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        if (n <= 0) {
            return 0;
        }
        
        long lo = 1;
        long hi = n;
        
        while (lo + 1 < hi) {
            long mid = lo + (hi - lo) / 2;
            boolean bad = isBadVersion((int) mid);
            if (bad) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        
        if (isBadVersion((int) lo)) {
            return (int) lo;
        }
        
        if (isBadVersion((int) hi)) {
            return (int) hi;
        }
        
        return 0;
    }
}

Update on 1/26/15:
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        if (n <= 0) {
            return -1;
        }
        
        int lo = 1;
        int hi = n;
        
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (isBadVersion(mid)) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        
        if (isBadVersion(lo)) {
            return lo;
        }
        
        return -1;
    }
}

No comments:

Post a Comment