First Bad Version | First Bad Version LeetCode Java | Programming Tutorials

preview_player
Показать описание
In this tutorial i have explained first bad version LeetCode solution using Binary Search in O(logn).

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.


NOTE : You should minimize the number of calls to the API.


Example:


Given n = 5, and version = 4 is the first bad version.


call isBadVersion(3) : false
call isBadVersion(5) : true
call isBadVersion(4) : true

Then 4 is the first bad version.

Рекомендации по теме
Комментарии
Автор

Its good to see the brute force then optimized one. Thanks for the video.

piyusharmaus
Автор

I like the you way describe the different approaches and dry run the code unlike others. Even your flow in English has improved over the last many videos. Last thing : Maza aagya bhai!

pahehepaa
Автор

I can do recursive binary search in (logn) time
int answer=Integer.MAX_VALUE;
public int doBinarySearch(int low, int high){
if(low<=high){
//int mid=(low+high)/2;
int mid=low+(high-low)/2;
if(isBadVersion(mid)){
answer=Math.min(answer, mid);
return doBinarySearch(low, mid-1);
}
else{
return doBinarySearch(mid+1, high);
}

}
return answer;

}
public int firstBadVersion(int n) {

doBinarySearch(1, n);
return answer;
}

praveenj
Автор

Why did you initiate end with n instead of n-1 ? And why start<end instead of start<=end?

emilyhuang
visit shbcf.ru