Leetcode 278:first Bad Version

Source: Internet
Author: User

You are 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 of the version is developed based on the previous version and all of the versions after a bad version are bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the "the" one, which causes all the following ones T O is bad.

You are are given an API bool Isbadversion (version) which would return whether version are bad. Implement a function to find the "the" You are should minimize the number of calls to the API.

This topic is very simple, direct two-point search

public class Solution extends VersionControl {public
    int firstbadversion (int n) {
    int lo = 1, hi = N;
    while (Lo < hi) {
        int med = lo + (hi-  lo)/2;
        if (Isbadversion (med)) {
            hi = med;
        } else {
            lo = med + 1;
        }
    }
    return lo;
    }
}

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.