Finish problem 0278-first-bad-version
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
def isBadVersion(version: int) -> bool:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def firstBadVersion(self, n: int) -> int:
|
||||||
|
lo, hi = 1, n
|
||||||
|
while lo < hi:
|
||||||
|
check = (hi + lo) // 2
|
||||||
|
if isBadVersion(check):
|
||||||
|
hi = check
|
||||||
|
else:
|
||||||
|
lo = check + 1
|
||||||
|
return lo
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import pytest
|
||||||
|
import solution
|
||||||
|
from solution import Solution
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"n,bad",
|
||||||
|
[
|
||||||
|
(5, 4),
|
||||||
|
(1, 1),
|
||||||
|
(10, 1),
|
||||||
|
(10, 10),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_first_bad_version(n, bad, monkeypatch):
|
||||||
|
monkeypatch.setattr(solution, "isBadVersion", lambda v: v >= bad)
|
||||||
|
assert Solution().firstBadVersion(n) == bad
|
||||||
Reference in New Issue
Block a user