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
相关推荐
TracyCoder1237 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
u0109272718 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_941837268 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
六义义9 小时前
java基础十二
java·数据结构·算法
四维碎片9 小时前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs9 小时前
C++与GPU计算(CUDA)
开发语言·c++·算法
独自破碎E10 小时前
【优先级队列】主持人调度(二)
算法
weixin_4454766810 小时前
leetCode每日一题——边反转的最小成本
算法·leetcode·职场和发展
打工的小王10 小时前
LeetCode Hot100(一)二分查找
算法·leetcode·职场和发展
Swift社区11 小时前
LeetCode 385 迷你语法分析器
算法·leetcode·职场和发展