You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this case the latest version of the product fails and we want to find the first bad version
So, I thought in the array like this [l pointer ->good,good,good,bad <- first bad version,bad,bad,bad,bad <- r pointer]
Used binSearch to find this point, going like this:
always set r pointer to mid version number when finding a bad version
if is not a bad version, move l pointer to mid + 1
Go back to step 1
🕒 Time and Space Complexity
Time: O(logn)
Space: O(1)
✅ Solution
classSolution
{
public:intfirstBadVersion(int n)
{
returnbinSearch(1, n);
}
intbinSearch(int l, int r)
{
while (l < r)
{
int m = l + (r - l) / 2;
if (isBadVersion(m))
{
r = m;
}
else
{
l = m + 1;
}
}
return r;
}
};