Created
May 1, 2020 12:14
-
-
Save Gowtham-369/db2ebdb8cc1174b5c37348380a2b0386 to your computer and use it in GitHub Desktop.
Day 1 : 30 days LeetCode May challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The API isBadVersion is defined for you. | |
// bool isBadVersion(int version); | |
class Solution { | |
public: | |
int firstBadVersion(int n) { | |
//searching for first Bad Version(true) | |
int l = 1; | |
int r = n; | |
int mid; | |
while(l<=r){ | |
mid = l+(r-l)/2; | |
if(!isBadVersion(mid)) | |
l = mid+1;//moves -->> from good | |
else | |
r = mid-1;//moves <<-- from bad | |
} | |
//Atlast,r points to last goodVersion and l points to First badversion | |
//At this point,they crossover and answer becomes l.. | |
return l; | |
} | |
//if(l<r) | |
//then r = mid | |
//then l and r points to same thing i.e first bad version | |
//go with below example | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment