Leetcode Note: First Bad Version

Source: Internet
Author: User

Leetcode Note: First Bad Version

I. Description

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 APIbool isBadVersion(version)Which will return whether version is bad. Implement a function to find the first bad version. You shoshould minimize the number of callto the API.

Ii. Question Analysis

I talked about a bunch of questions and didn't know what I was trying to do at the beginning. Then I checked the question on the Internet and referenced it as follows:

You are a product manager and lead the team to develop a new product. Unfortunately, the final version of the product does not pass the quality check. Because each version is developed based on the previous version, all versions after a corrupted version are corrupted.

Suppose there are n versions [1, 2 ,..., N], now you need to find the first corrupted version, which causes all later versions to break down.

Provide you with an APIbool isBadVersion(version)The function is to return whether a version is damaged. Implement a function to find the first corrupted version. You should minimize the number of API calls.

Originally, only functions similar to Search for a Range can be implemented to determine bad versions.bool isBadVersion(version)The question has been provided, so you don't have to worry about how to do it. Here we still use binary lookup to solve the problem.

Iii. Sample Code

At the beginning of the year, the following code always prompts: Time Limit Exceeded

// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public:    int firstBadVersion(int n) {        int low = 1, high = n;        int midIndex = 0;        while (low <= high)        {            midIndex = (low + high) / 2;            if (isBadVersion(midIndex))                high = midIndex - 1;            else                low = midIndex + 1;        }        return low;    }};

After checking for a period of time, it is found that when n gets a large number, a problem occurs. It turns out to be a statement.midIndex = (low + high) / 2;Overflow when directly adding, resulting in loop timeout. Use the following code to Accept.

// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public:    int firstBadVersion(int n) {        int low = 1, high = n;        int midIndex = 0;        while (low <= high)        {            midIndex = low + (high - low) / 2;            if (isBadVersion(midIndex))                high = midIndex - 1;            else                low = midIndex + 1;        }        return low;    }};

Iv. Summary

The difficulty of this question is under Search for a Range, but there is a data overflow problem. Therefore, we cannot take simple questions at all times.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.