Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed May 21, 2024
1 parent f4c1c76 commit e17f493
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions leetcode/BinarySearch/278.FirstBadVersion/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// https://leetcode.com/problems/first-bad-version

package main

import (
"fmt"
)

// Assume this is a pre-defined API that checks if a version is bad
var firstBad int

func isBadVersion(version int) bool {
return version >= firstBad
}

// FirstBadVersion finds the first bad version among n versions
func firstBadVersion(n int) int {
left, right := 1, n
for left < right {
mid := left + (right-left)>>1
if isBadVersion(mid) {
right = mid
} else {
left = mid + 1
}
}
return left
}

func main() {
n := 5
firstBad = 4
fmt.Println(firstBadVersion(n))
}

0 comments on commit e17f493

Please sign in to comment.