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
相关推荐
前端 贾公子4 分钟前
详解 LeetCode 第 242 题 - 有效的字母组
算法·leetcode·职场和发展
sofaraway1310 分钟前
【多目标进化算法】 MOEA/D算法(知识点)
算法
pystraf31 分钟前
UOJ 228 基础数据结构练习题 Solution
数据结构·c++·算法·线段树
海底火旺40 分钟前
破解二维矩阵搜索难题:从暴力到最优的算法之旅
javascript·算法·面试
黄昏ivi2 小时前
电力系统最小惯性常数解析
算法
独家回忆3642 小时前
每日算法-250425
算法
烁3472 小时前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
Demons_kirit2 小时前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
软行3 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
永远在Debug的小殿下3 小时前
查找函数【C++】
数据结构·算法