Leetcode 278. First Bad Version

Problem

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Algorithm

Bineary search.

Code

python3 复制代码
class Solution:
    def firstBadVersion(self, n: int) -> int:
        L, R, = 0, n - 1
        while L < R:
            Mid = (L + R + 1) // 2
            if isBadVersion(Mid):
                R = Mid - 1
            else: L = Mid
        return L + 1
相关推荐
TracyCoder12320 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
u0109272711 天前
C++中的策略模式变体
开发语言·c++·算法
2501_941837261 天前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
六义义1 天前
java基础十二
java·数据结构·算法
四维碎片1 天前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs1 天前
C++与GPU计算(CUDA)
开发语言·c++·算法
独自破碎E1 天前
【优先级队列】主持人调度(二)
算法
weixin_445476681 天前
leetCode每日一题——边反转的最小成本
算法·leetcode·职场和发展
打工的小王1 天前
LeetCode Hot100(一)二分查找
算法·leetcode·职场和发展
Swift社区1 天前
LeetCode 385 迷你语法分析器
算法·leetcode·职场和发展