Last active
February 7, 2023 20:29
-
-
Save tchnorider/f563d0c625a311766451747f3e7b01fb to your computer and use it in GitHub Desktop.
Returns first bad version
This file contains hidden or 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
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