Leetcode # 278. First Bad Version

https://leetcode.com/problems/first-bad-version/

Solution

Time Complexity: O(log(n))
Space Complexity: O(1)

class Solution:
  def firstBadVersion(self, n: int) -> int:
    if isBadVersion(1):
      return 1
    good, bad = 1, n
    while bad - good > 1:
      mid = (good + bad) // 2
      if isBadVersion(mid):
        bad = mid
      else: 
        good = mid
    return bad

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami