2
头图

The first wrong version

Topic description: You are a product manager, currently leading a team to develop a new product. Unfortunately, the latest version of your product did not pass the quality inspection. Since each version is developed based on the previous version, all versions after the wrong version are wrong.

Suppose you have n versions [1, 2, ..., n], and you want to find the first wrong version that caused all subsequent versions to go wrong.

You can call the bool isBadVersion(version) interface to determine whether the version number version is wrong in the unit test. Implement a function to find the first wrong version. You should minimize the number of calls to the API.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/first-bad-version/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Dichotomy

Record the first element and the last element with low and high respectively, and then use the dichotomy to solve the problem. The solving process is as follows:

  • Use mid to record the middle position, that is, low + (high - low) / 2 , and then call the isBadVersion(mid) method to determine whether the current version is the wrong version;
  • If the current version is the wrong version and the previous version (the isBadVersion method needs to be called again) is also the wrong version, reset high to mid-1 , and then proceed to the next round of processing;
  • If the current version is the wrong version but the previous version of the current version is not the wrong version, the current version is the first wrong version, and the current version is returned;
  • If the current version is not the wrong version, reset low to mid+1 , and then proceed to the next round of processing.
  • The condition for the end of the loop is that low is greater than high.

Finally, if the wrong version is not found, -1 is returned.

public class LeetCode_278 extends VersionControl {
    /**
     * 二分法
     *
     * @param n
     * @return
     */
    public static int firstBadVersion(int n) {
        int low = 1, high = n;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (isBadVersion(mid)) {
                if (mid > low && isBadVersion(mid - 1)) {
                    high = mid - 1;
                } else {
                    return mid;
                }
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(firstBadVersion(2126753390));
    }
}

class VersionControl {
    /**
     * 测试数据,前1702766718个版本都不是错误版本,从第1702766719个版本开始都是错误版本
     */
    public static boolean isBadVersion(int version) {
        if (version < 1702766719) {
            return false;
        } else {
            return true;
        }
    }
}
[Daily Message] If there is a shortcut to success, the road must be persistence.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!