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
相关推荐
风筝在晴天搁浅41 分钟前
代码随想录 718.最长重复子数组
算法
kyle~1 小时前
算法---回溯算法
算法
star _chen1 小时前
C++实现完美洗牌算法
开发语言·c++·算法
hzxxxxxxx1 小时前
1234567
算法
Sylvia-girl2 小时前
数据结构之复杂度
数据结构·算法
CQ_YM2 小时前
数据结构之队列
c语言·数据结构·算法·
VekiSon2 小时前
数据结构与算法——树和哈希表
数据结构·算法
大江东去浪淘尽千古风流人物3 小时前
【DSP】向量化操作的误差来源分析及其经典解决方案
linux·运维·人工智能·算法·vr·dsp开发·mr
Unstoppable224 小时前
代码随想录算法训练营第 56 天 | 拓扑排序精讲、Dijkstra(朴素版)精讲
java·数据结构·算法·
饕餮怪程序猿4 小时前
A*算法(C++实现)
开发语言·c++·算法