Skip to content

Commit 57b6cd7

Browse files
authored
feat: add more leetcode problems (#31)
1 parent 9199d8c commit 57b6cd7

File tree

214 files changed

+8969
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+8969
-78
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"problem_name": "balanced_binary_tree",
3+
"solution_class_name": "Solution",
4+
"problem_number": "110",
5+
"problem_title": "Balanced Binary Tree",
6+
"difficulty": "Easy",
7+
"topics": "Tree, Depth-First Search, Binary Tree",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given a binary tree, determine if it is **height-balanced**.\n\nA height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.",
10+
"readme_examples": [
11+
{
12+
"content": "![Example 1](https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg)\n\n```\nInput: root = [3,9,20,null,null,15,7]\nOutput: true\n```"
13+
},
14+
{
15+
"content": "![Example 2](https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg)\n\n```\nInput: root = [1,2,2,3,3,null,null,4,4]\nOutput: false\n```"
16+
},
17+
{ "content": "```\nInput: root = []\nOutput: true\n```" }
18+
],
19+
"readme_constraints": "- The number of nodes in the tree is in the range `[0, 5000]`.\n- `-10^4 <= Node.val <= 10^4`",
20+
"readme_additional": "",
21+
"solution_imports": "from leetcode_py import TreeNode",
22+
"solution_methods": [
23+
{
24+
"name": "is_balanced",
25+
"parameters": "root: TreeNode | None",
26+
"return_type": "bool",
27+
"dummy_return": "False"
28+
}
29+
],
30+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom leetcode_py import TreeNode\nfrom .solution import Solution",
31+
"test_class_name": "BalancedBinaryTree",
32+
"test_helper_methods": [
33+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
34+
],
35+
"test_methods": [
36+
{
37+
"name": "test_is_balanced",
38+
"parametrize": "root_list, expected",
39+
"parametrize_typed": "root_list: list[int | None], expected: bool",
40+
"test_cases": "[([3, 9, 20, None, None, 15, 7], True), ([1, 2, 2, 3, 3, None, None, 4, 4], False), ([], True)]",
41+
"body": "root = TreeNode.from_list(root_list)\nresult = self.solution.is_balanced(root)\nassert result == expected"
42+
}
43+
],
44+
"playground_imports": "from solution import Solution\nfrom leetcode_py import TreeNode",
45+
"playground_test_case": "# Example test case\nroot_list = [3, 9, 20, None, None, 15, 7]\nroot = TreeNode.from_list(root_list)\nexpected = True",
46+
"playground_execution": "result = Solution().is_balanced(root)\nresult",
47+
"playground_assertion": "assert result == expected"
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"problem_name": "basic_calculator",
3+
"solution_class_name": "Solution",
4+
"problem_number": "224",
5+
"problem_title": "Basic Calculator",
6+
"difficulty": "Hard",
7+
"topics": "Math, String, Stack, Recursion",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.\n\n**Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`.",
10+
"readme_examples": [
11+
{ "content": "```\nInput: s = \"1 + 1\"\nOutput: 2\n```" },
12+
{ "content": "```\nInput: s = \" 2-1 + 2 \"\nOutput: 3\n```" },
13+
{ "content": "```\nInput: s = \"(1+(4+5+2)-3)+(6+8)\"\nOutput: 23\n```" }
14+
],
15+
"readme_constraints": "- `1 <= s.length <= 3 * 10^5`\n- `s` consists of digits, `'+'`, `'-'`, `'('`, `')'`, and `' '`.\n- `s` represents a valid expression.\n- `'+'` is **not** used as a unary operation (i.e., `\"+1\"` and `\"+(2 + 3)\"` is invalid).\n- `'-'` could be used as a unary operation (i.e., `\"-1\"` and `\"-(2 + 3)\"` is valid).\n- There will be no two consecutive operators in the input.\n- Every number and running calculation will fit in a signed 32-bit integer.",
16+
"readme_additional": "",
17+
"solution_imports": "",
18+
"solution_methods": [
19+
{ "name": "calculate", "parameters": "s: str", "return_type": "int", "dummy_return": "0" }
20+
],
21+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
22+
"test_class_name": "BasicCalculator",
23+
"test_helper_methods": [
24+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
25+
],
26+
"test_methods": [
27+
{
28+
"name": "test_calculate",
29+
"parametrize": "s, expected",
30+
"parametrize_typed": "s: str, expected: int",
31+
"test_cases": "[(\"1 + 1\", 2), (\" 2-1 + 2 \", 3), (\"(1+(4+5+2)-3)+(6+8)\", 23), (\"1\", 1), (\"-1\", -1), (\"-(1+2)\", -3), (\"2147483647\", 2147483647), (\"1-1+1\", 1)]",
32+
"body": "result = self.solution.calculate(s)\nassert result == expected"
33+
}
34+
],
35+
"playground_imports": "from solution import Solution",
36+
"playground_test_case": "# Example test case\ns = '(1+(4+5+2)-3)+(6+8)'\nexpected = 23",
37+
"playground_execution": "result = Solution().calculate(s)\nresult",
38+
"playground_assertion": "assert result == expected"
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"problem_name": "best_time_to_buy_and_sell_stock",
3+
"solution_class_name": "Solution",
4+
"problem_number": "121",
5+
"problem_title": "Best Time to Buy and Sell Stock",
6+
"difficulty": "Easy",
7+
"topics": "Array, Dynamic Programming",
8+
"tags": ["grind-75"],
9+
"readme_description": "You are given an array `prices` where `prices[i]` is the price of a given stock on the ith day.\n\nYou want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.\n\nReturn *the maximum profit you can achieve from this transaction*. If you cannot achieve any profit, return `0`.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: prices = [7,1,5,3,6,4]\nOutput: 5\n```\n**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.\nNote that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell."
13+
},
14+
{
15+
"content": "```\nInput: prices = [7,6,4,3,1]\nOutput: 0\n```\n**Explanation:** In this case, no transactions are done and the max profit = 0."
16+
}
17+
],
18+
"readme_constraints": "- 1 <= prices.length <= 10^5\n- 0 <= prices[i] <= 10^4",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{
23+
"name": "max_profit",
24+
"parameters": "prices: list[int]",
25+
"return_type": "int",
26+
"dummy_return": "0"
27+
}
28+
],
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
30+
"test_class_name": "BestTimeToBuyAndSellStock",
31+
"test_helper_methods": [
32+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
33+
],
34+
"test_methods": [
35+
{
36+
"name": "test_max_profit",
37+
"parametrize": "prices, expected",
38+
"parametrize_typed": "prices: list[int], expected: int",
39+
"test_cases": "[([7, 1, 5, 3, 6, 4], 5), ([7, 6, 4, 3, 1], 0), ([1, 2, 3, 4, 5], 4), ([5, 4, 3, 2, 1], 0), ([1], 0), ([2, 1], 0), ([1, 2], 1), ([3, 2, 6, 5, 0, 3], 4)]",
40+
"body": "result = self.solution.max_profit(prices)\nassert result == expected"
41+
}
42+
],
43+
"playground_imports": "from solution import Solution",
44+
"playground_test_case": "# Example test case\nprices = [7, 1, 5, 3, 6, 4]\nexpected = 5",
45+
"playground_execution": "result = Solution().max_profit(prices)\nresult",
46+
"playground_assertion": "assert result == expected"
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"problem_name": "binary_tree_level_order_traversal",
3+
"solution_class_name": "Solution",
4+
"problem_number": "102",
5+
"problem_title": "Binary Tree Level Order Traversal",
6+
"difficulty": "Medium",
7+
"topics": "Tree, Breadth-First Search, Binary Tree",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given the `root` of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).",
10+
"readme_examples": [
11+
{
12+
"content": "![Example 1](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)\n\n```\nInput: root = [3,9,20,null,null,15,7]\nOutput: [[3],[9,20],[15,7]]\n```"
13+
},
14+
{ "content": "```\nInput: root = [1]\nOutput: [[1]]\n```" },
15+
{ "content": "```\nInput: root = []\nOutput: []\n```" }
16+
],
17+
"readme_constraints": "- The number of nodes in the tree is in the range [0, 2000]\n- -1000 <= Node.val <= 1000",
18+
"readme_additional": "",
19+
"solution_imports": "from leetcode_py import TreeNode",
20+
"solution_methods": [
21+
{
22+
"name": "level_order",
23+
"parameters": "root: TreeNode | None",
24+
"return_type": "list[list[int]]",
25+
"dummy_return": "[]"
26+
}
27+
],
28+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom leetcode_py import TreeNode\nfrom .solution import Solution",
29+
"test_class_name": "BinaryTreeLevelOrderTraversal",
30+
"test_helper_methods": [
31+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
32+
],
33+
"test_methods": [
34+
{
35+
"name": "test_level_order",
36+
"parametrize": "root_list, expected",
37+
"parametrize_typed": "root_list: list[int | None], expected: list[list[int]]",
38+
"test_cases": "[([3, 9, 20, None, None, 15, 7], [[3], [9, 20], [15, 7]]), ([1], [[1]]), ([], []), ([1, 2, 3, 4, 5, 6, 7], [[1], [2, 3], [4, 5, 6, 7]]), ([1, 2, None, 3, None, 4, None, 5], [[1], [2], [3], [4], [5]])]",
39+
"body": "root = TreeNode.from_list(root_list) if root_list else None\nresult = self.solution.level_order(root)\nassert result == expected"
40+
}
41+
],
42+
"playground_imports": "from solution import Solution\nfrom leetcode_py import TreeNode",
43+
"playground_test_case": "# Example test case\nroot_list = [3, 9, 20, None, None, 15, 7]\nroot = TreeNode.from_list(root_list)\nexpected = [[3], [9, 20], [15, 7]]",
44+
"playground_execution": "result = Solution().level_order(root)\nresult",
45+
"playground_assertion": "assert result == expected"
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"problem_name": "climbing_stairs",
3+
"solution_class_name": "Solution",
4+
"problem_number": "70",
5+
"problem_title": "Climbing Stairs",
6+
"difficulty": "Easy",
7+
"topics": "Math, Dynamic Programming, Memoization",
8+
"tags": ["grind-75"],
9+
"readme_description": "You are climbing a staircase. It takes `n` steps to reach the top.\n\nEach time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: n = 2\nOutput: 2\n```\n**Explanation:** There are two ways to climb to the top.\n1. 1 step + 1 step\n2. 2 steps"
13+
},
14+
{
15+
"content": "```\nInput: n = 3\nOutput: 3\n```\n**Explanation:** There are three ways to climb to the top.\n1. 1 step + 1 step + 1 step\n2. 1 step + 2 steps\n3. 2 steps + 1 step"
16+
}
17+
],
18+
"readme_constraints": "- 1 <= n <= 45",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{ "name": "climb_stairs", "parameters": "n: int", "return_type": "int", "dummy_return": "1" }
23+
],
24+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
25+
"test_class_name": "ClimbingStairs",
26+
"test_helper_methods": [
27+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
28+
],
29+
"test_methods": [
30+
{
31+
"name": "test_climb_stairs",
32+
"parametrize": "n, expected",
33+
"parametrize_typed": "n: int, expected: int",
34+
"test_cases": "[(1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (10, 89), (20, 10946), (45, 1836311903)]",
35+
"body": "result = self.solution.climb_stairs(n)\nassert result == expected"
36+
}
37+
],
38+
"playground_imports": "from solution import Solution",
39+
"playground_test_case": "# Example test case\nn = 3\nexpected = 3",
40+
"playground_execution": "result = Solution().climb_stairs(n)\nresult",
41+
"playground_assertion": "assert result == expected"
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"problem_name": "combination_sum",
3+
"solution_class_name": "Solution",
4+
"problem_number": "39",
5+
"problem_title": "Combination Sum",
6+
"difficulty": "Medium",
7+
"topics": "Array, Backtracking",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an array of **distinct** integers `candidates` and a target integer `target`, return *a list of all **unique combinations** of* `candidates` *where the chosen numbers sum to* `target`. You may return the combinations in **any order**.\n\nThe **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different.\n\nThe test cases are generated such that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: candidates = [2,3,6,7], target = 7\nOutput: [[2,2,3],[7]]\n```\n**Explanation:** 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations."
13+
},
14+
{
15+
"content": "```\nInput: candidates = [2,3,5], target = 8\nOutput: [[2,2,2,2],[2,3,3],[3,5]]\n```"
16+
},
17+
{ "content": "```\nInput: candidates = [2], target = 1\nOutput: []\n```" }
18+
],
19+
"readme_constraints": "- 1 <= candidates.length <= 30\n- 2 <= candidates[i] <= 40\n- All elements of candidates are distinct.\n- 1 <= target <= 40",
20+
"readme_additional": "",
21+
"solution_imports": "",
22+
"solution_methods": [
23+
{
24+
"name": "combination_sum",
25+
"parameters": "candidates: list[int], target: int",
26+
"return_type": "list[list[int]]",
27+
"dummy_return": "[]"
28+
}
29+
],
30+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
31+
"test_class_name": "CombinationSum",
32+
"test_helper_methods": [
33+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
34+
],
35+
"test_methods": [
36+
{
37+
"name": "test_combination_sum",
38+
"parametrize": "candidates, target, expected",
39+
"parametrize_typed": "candidates: list[int], target: int, expected: list[list[int]]",
40+
"test_cases": "[([2, 3, 6, 7], 7, [[2, 2, 3], [7]]), ([2, 3, 5], 8, [[2, 2, 2, 2], [2, 3, 3], [3, 5]]), ([2], 1, [])]",
41+
"body": "result = self.solution.combination_sum(candidates, target)\n# Sort both result and expected for comparison\nresult_sorted = [sorted(combo) for combo in result]\nexpected_sorted = [sorted(combo) for combo in expected]\nresult_sorted.sort()\nexpected_sorted.sort()\nassert result_sorted == expected_sorted"
42+
}
43+
],
44+
"playground_imports": "from solution import Solution",
45+
"playground_test_case": "# Example test case\ncandidates = [2, 3, 6, 7]\ntarget = 7\nexpected = [[2, 2, 3], [7]]",
46+
"playground_execution": "result = Solution().combination_sum(candidates, target)\nresult",
47+
"playground_assertion": "# Sort for comparison\nresult_sorted = [sorted(combo) for combo in result]\nexpected_sorted = [sorted(combo) for combo in expected]\nresult_sorted.sort()\nexpected_sorted.sort()\nassert result_sorted == expected_sorted"
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"problem_name": "contains_duplicate",
3+
"solution_class_name": "Solution",
4+
"problem_number": "217",
5+
"problem_title": "Contains Duplicate",
6+
"difficulty": "Easy",
7+
"topics": "Array, Hash Table, Sorting",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: nums = [1,2,3,1]\nOutput: true\n```\n**Explanation:** The element 1 occurs at the indices 0 and 3."
13+
},
14+
{
15+
"content": "```\nInput: nums = [1,2,3,4]\nOutput: false\n```\n**Explanation:** All elements are distinct."
16+
},
17+
{ "content": "```\nInput: nums = [1,1,1,3,3,4,3,2,4,2]\nOutput: true\n```" }
18+
],
19+
"readme_constraints": "- 1 <= nums.length <= 10^5\n- -10^9 <= nums[i] <= 10^9",
20+
"readme_additional": "",
21+
"solution_imports": "",
22+
"solution_methods": [
23+
{
24+
"name": "contains_duplicate",
25+
"parameters": "nums: list[int]",
26+
"return_type": "bool",
27+
"dummy_return": "False"
28+
}
29+
],
30+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
31+
"test_class_name": "ContainsDuplicate",
32+
"test_helper_methods": [
33+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
34+
],
35+
"test_methods": [
36+
{
37+
"name": "test_contains_duplicate",
38+
"parametrize": "nums, expected",
39+
"parametrize_typed": "nums: list[int], expected: bool",
40+
"test_cases": "[([1, 2, 3, 1], True), ([1, 2, 3, 4], False), ([1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True)]",
41+
"body": "result = self.solution.contains_duplicate(nums)\nassert result == expected"
42+
}
43+
],
44+
"playground_imports": "from solution import Solution",
45+
"playground_test_case": "# Example test case\nnums = [1, 2, 3, 1]\nexpected = True",
46+
"playground_execution": "result = Solution().contains_duplicate(nums)\nresult",
47+
"playground_assertion": "assert result == expected"
48+
}

0 commit comments

Comments
 (0)