Skip to main content

278 - First Bad Version

Details

KeyValue
Linkhttps://leetcode.com/problems/first-bad-version/
LanguagePython 3
Runtime44 ms, faster than 51.43% of Python3 online submissions for First Bad Version
Memory Usage13.9 MB, less than 60.55% of Python3 online submissions for First Bad Version
Datastructuresint
AlgorithmsBinary Search (with two pointers)
ComplexityTime: O(NlogN), Space: O(1)

Procedure

  1. ...

Code

# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
left, right = 1, n
while left < right:
middle = left + (right-left) // 2
if isBadVersion(middle): right = middle
else: left = middle + 1
return left