LeetCode 278 First Bad Version (First Bad Version) (bipartite )(*)

Source: Internet
Author: User

LeetCode 278 First Bad Version (First Bad Version) (bipartite )(*)
Translation

You are a product manager and are currently leading the team to develop a new product. Unfortunately, the previous version of the product did not pass the quality check. Because each version is developed based on the previous version, versions after the bad version are also bad. Suppose you have n versions [1, 2,..., n], and you want to find the first bad version that causes all later versions to deteriorate. Give you an API -- bool isBadVersion (version), which can determine whether a version is bad. Implement a function to find the first bad version. You should call this API as little as possible.
Original
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.
Analysis

As a matter of fact, we have mentioned so many questions. To solve this problem, we only need to use the bipartite method, but it is very error-prone in the middle.

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

I wrote the sixth question today, so tired ......

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.