|
| 1 | +{ |
| 2 | + "problem_name": "first_bad_version", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "278", |
| 5 | + "problem_title": "First Bad Version", |
| 6 | + "difficulty": "Easy", |
| 7 | + "topics": "Binary Search, Interactive", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "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.\n\nSuppose 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.\n\nYou 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.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput: n = 5, bad = 4\nOutput: 4\n```\n**Explanation:**\n```\ncall isBadVersion(3) -> false\ncall isBadVersion(5) -> true\ncall isBadVersion(4) -> true\n```\nThen 4 is the first bad version." |
| 13 | + }, |
| 14 | + { "content": "```\nInput: n = 1, bad = 1\nOutput: 1\n```" } |
| 15 | + ], |
| 16 | + "readme_constraints": "- 1 <= bad <= n <= 2^31 - 1", |
| 17 | + "readme_additional": "**Note:** The `isBadVersion` API is already defined for you.", |
| 18 | + "solution_imports": "", |
| 19 | + "solution_methods": [ |
| 20 | + { |
| 21 | + "name": "is_bad_version", |
| 22 | + "parameters": "version: int", |
| 23 | + "return_type": "bool", |
| 24 | + "dummy_return": "False" |
| 25 | + }, |
| 26 | + { |
| 27 | + "name": "first_bad_version", |
| 28 | + "parameters": "n: int", |
| 29 | + "return_type": "int", |
| 30 | + "dummy_return": "1" |
| 31 | + } |
| 32 | + ], |
| 33 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution", |
| 34 | + "test_class_name": "FirstBadVersion", |
| 35 | + "test_helper_methods": [ |
| 36 | + { "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }, |
| 37 | + { |
| 38 | + "name": "is_bad_version", |
| 39 | + "parameters": "version: int, bad: int", |
| 40 | + "body": "return version >= bad" |
| 41 | + } |
| 42 | + ], |
| 43 | + "test_methods": [ |
| 44 | + { |
| 45 | + "name": "test_first_bad_version", |
| 46 | + "parametrize": "n, bad, expected", |
| 47 | + "parametrize_typed": "n: int, bad: int, expected: int", |
| 48 | + "test_cases": "[(5, 4, 4), (1, 1, 1), (3, 1, 1), (10, 7, 7), (5, -1, 1)]", |
| 49 | + "body": "solution = Solution()\n# Mock is_bad_version for this test\nsolution.is_bad_version = lambda version: self.is_bad_version(version, bad)\nresult = solution.first_bad_version(n)\nassert result == expected" |
| 50 | + } |
| 51 | + ], |
| 52 | + "playground_imports": "from solution import Solution", |
| 53 | + "playground_test_case": "# Example test case\nn = 5\nbad = 4\nexpected = 4", |
| 54 | + "playground_execution": "solution = Solution()\nsolution.is_bad_version = lambda version: version >= bad\nresult = solution.first_bad_version(n)\nresult", |
| 55 | + "playground_assertion": "assert result == expected" |
| 56 | +} |
0 commit comments