Skip to content

Instantly share code, notes, and snippets.

@tchnorider
Last active February 7, 2023 20:29
Show Gist options
  • Save tchnorider/f563d0c625a311766451747f3e7b01fb to your computer and use it in GitHub Desktop.
Save tchnorider/f563d0c625a311766451747f3e7b01fb to your computer and use it in GitHub Desktop.
Returns first bad version
public class BadVersion {
public int badOne = 1;
// AC
public int firstBadVersion(int n) {
int right = n;
int left = 1;
while(left < right) {
int half = left + (right - left) / 2;
if(isBadVersion(half)) {
right = half;
} else {
left = half + 1;
}
}
return left;
}
// HELPER
public Boolean isBadVersion(int n) {
if(n > 1) {
if(badOne== n){
return true;
} else if(n > badOne){
return true;
} else {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment