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
相关推荐
2401_894828121 小时前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
Remember_9931 小时前
【LeetCode精选算法】前缀和专题二
算法·哈希算法·散列表
源代码•宸1 小时前
Leetcode—509. 斐波那契数【简单】
经验分享·算法·leetcode·面试·golang·记忆化搜索·动规
博大世界2 小时前
matlab结构体数组定义
数据结构·算法
Loo国昌2 小时前
【LangChain1.0】第九阶段:文档处理工程 (LlamaIndex)
人工智能·后端·python·算法·langchain
Zach_yuan2 小时前
面向对象封装线程:用 C++ 封装 pthread
开发语言·c++·算法
踩坑记录3 小时前
leetcode hot100 206.反转链表 easy
leetcode
安特尼3 小时前
X 推荐算法分析
算法·机器学习·推荐算法
罗湖老棍子4 小时前
强迫症冒险家的任务清单:字典序最小拓扑排序
数据结构·算法·图论·拓扑排序
不穿格子的程序员5 小时前
从零开始写算法——回溯篇4:分割回文串 + N皇后
算法·深度优先·dfs