Skip to content

Instantly share code, notes, and snippets.

@munguial
Created May 2, 2020 05:14
Show Gist options
  • Save munguial/7d8aecceed11b539732482b84ddfe100 to your computer and use it in GitHub Desktop.
Save munguial/7d8aecceed11b539732482b84ddfe100 to your computer and use it in GitHub Desktop.
Day 1 - First Bad Version
# https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3316/
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):
class Solution:
def firstBadVersion(self, n):
return self.bs(1, n)
def bs(self, a, b):
if a > b:
return b if isBadVersion(b) else a
m = int((a + b) / 2)
if isBadVersion(m):
return self.bs(a, m - 1)
else:
return self.bs(m + 1, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment