-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 0 ms (100.00%), Space: 1.9 MB (85.10%) - LeetHub
- Loading branch information
1 parent
26d4fc1
commit a9d8d26
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Forward declaration of isBadVersion API. | ||
* @param version your guess about first bad version | ||
* @return true if current version is bad | ||
* false if current version is good | ||
* func isBadVersion(version int) bool; | ||
*/ | ||
|
||
func firstBadVersion(n int) int { | ||
// binary search iterative | ||
|
||
left, right := 1, n | ||
|
||
for left <= right { | ||
pivot := left + (right - left)/2 | ||
|
||
if isBadVersion(pivot) == true && pivot == 1 { | ||
return 1 | ||
} else if isBadVersion(pivot) == true && isBadVersion(pivot - 1) == false { | ||
return pivot | ||
} | ||
|
||
if isBadVersion(pivot) == false { | ||
left = pivot + 1 | ||
} else { | ||
right = pivot - 1 | ||
} | ||
} | ||
|
||
return 1 | ||
} |