All questions
EasyBinary Search
First Bad Version
Written by a community member · · 1 attempt · 100% pass
0
You are given versions numbered from 1 through n. Once a version is bad, every later version is also bad. The version-control API has already been queried for you: is_bad[i] is True when version i + 1 is bad and False otherwise.
Implement first_bad_version(is_bad) to return the smallest bad version while looking at as few entries as possible. Your algorithm should use binary search and make O(log n) lookups.
Assume 1 <= n and that at least one version is bad. The answers are monotonic: if version v is bad, every version greater than v is bad.
Example 1:
Input: is_bad = [False, False, False, True, True] (n = 5, bad version 4)
Output: 4
Example 2:
Input: is_bad = [True]
Output: 1