Modified binary search (logn) for first bad version


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

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.