forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
278.c
42 lines (34 loc) · 807 Bytes
/
278.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#define BAD 3 // for testing
// Forward declaration of isBadVersion API.
bool isBadVersion(int version) {
return (version >= BAD);
}
int firstBadVersion(int n) {
if (n <= 1) return n;
int low = 1;
int high = n;
int mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (isBadVersion(mid)) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return low;
}
int main() {
assert(firstBadVersion(3) == 3);
assert(firstBadVersion(4) == 3);
assert(firstBadVersion(5) == 3);
assert(firstBadVersion(6) == 3);
assert(firstBadVersion(7) == 3);
assert(firstBadVersion(8) == 3);
assert(firstBadVersion(9) == 3);
return 0;
}