Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created April 30, 2020 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkorg/ae6cce45dd472bed97dc15138b5420cd to your computer and use it in GitHub Desktop.
Save adamkorg/ae6cce45dd472bed97dc15138b5420cd to your computer and use it in GitHub Desktop.
Leetcode 278: First Bad Version
#include <iostream>
using namespace std;
//leetcode supplied function (I've created an example):
bool isBadVersion(int n) {
return (n >= 4);
}
int firstBadVersion(int n) {
int l=1, r=n, m=l+(r-l)/2;
while (l <= r) {
m = l+(r-l)/2;
if (isBadVersion(m)) r = m-1; //go left
else l=m+1; //go right
}
if (!isBadVersion(m)) m++;
return m;
}
int main() {
int n = 10;
cout << firstBadVersion(n) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment