Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 1.9 MB (85.10%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ardinusawan committed Jul 9, 2022
1 parent 26d4fc1 commit a9d8d26
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 278-first-bad-version/278-first-bad-version.go
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
}

0 comments on commit a9d8d26

Please sign in to comment.