first_bad_version

 1# The isBadVersion API is already defined for you.
 2def isBadVersion(version: int) -> bool:
 3    return True
 4
 5
 6# @leet start
 7class Solution:
 8    def firstBadVersion(self, n: int) -> int:
 9        """
10        This question asks us to find the first bad version introduced into
11        a project, where the versions are from 1 to n and at some point, a
12        change was introduced which broke the project. We can check if the project
13        is a bad version with the function `isBadVersion`.
14
15        We can do this by binary searching, to solve this in $O(log n)$ time.
16        This is done by setting our left and right pointers to 1 and n, and
17        then checking if the midpoint is a bad version. If it is, we set our
18        right pointer to mid. Otherwise, we set our left pointer to mid + 1
19        since we know that mid + 1 is the first possible bad version.
20        We have to set r to m in the case since we don't know if m - 1 is a bad
21        version.
22        """
23        l = 1
24        r = n
25
26        while l < r:
27            m = (l + r) // 2
28            if isBadVersion(m):
29                r = m
30            else:
31                l = m + 1
32        return l
33
34
35# @leet end
36
37
38def test():
39    assert 2 + 2 == 4
def isBadVersion(version: int) -> bool:
3def isBadVersion(version: int) -> bool:
4    return True
class Solution:
 8class Solution:
 9    def firstBadVersion(self, n: int) -> int:
10        """
11        This question asks us to find the first bad version introduced into
12        a project, where the versions are from 1 to n and at some point, a
13        change was introduced which broke the project. We can check if the project
14        is a bad version with the function `isBadVersion`.
15
16        We can do this by binary searching, to solve this in $O(log n)$ time.
17        This is done by setting our left and right pointers to 1 and n, and
18        then checking if the midpoint is a bad version. If it is, we set our
19        right pointer to mid. Otherwise, we set our left pointer to mid + 1
20        since we know that mid + 1 is the first possible bad version.
21        We have to set r to m in the case since we don't know if m - 1 is a bad
22        version.
23        """
24        l = 1
25        r = n
26
27        while l < r:
28            m = (l + r) // 2
29            if isBadVersion(m):
30                r = m
31            else:
32                l = m + 1
33        return l
def firstBadVersion(self, n: int) -> int:
 9    def firstBadVersion(self, n: int) -> int:
10        """
11        This question asks us to find the first bad version introduced into
12        a project, where the versions are from 1 to n and at some point, a
13        change was introduced which broke the project. We can check if the project
14        is a bad version with the function `isBadVersion`.
15
16        We can do this by binary searching, to solve this in $O(log n)$ time.
17        This is done by setting our left and right pointers to 1 and n, and
18        then checking if the midpoint is a bad version. If it is, we set our
19        right pointer to mid. Otherwise, we set our left pointer to mid + 1
20        since we know that mid + 1 is the first possible bad version.
21        We have to set r to m in the case since we don't know if m - 1 is a bad
22        version.
23        """
24        l = 1
25        r = n
26
27        while l < r:
28            m = (l + r) // 2
29            if isBadVersion(m):
30                r = m
31            else:
32                l = m + 1
33        return l

This question asks us to find the first bad version introduced into a project, where the versions are from 1 to n and at some point, a change was introduced which broke the project. We can check if the project is a bad version with the function isBadVersion.

We can do this by binary searching, to solve this in $O(log n)$ time. This is done by setting our left and right pointers to 1 and n, and then checking if the midpoint is a bad version. If it is, we set our right pointer to mid. Otherwise, we set our left pointer to mid + 1 since we know that mid + 1 is the first possible bad version. We have to set r to m in the case since we don't know if m - 1 is a bad version.

def test():
39def test():
40    assert 2 + 2 == 4