Skip to content

Commit ca892d7

Browse files
committed
feat: add partition_equal_subset_sum
1 parent 740592b commit ca892d7

Some content is hidden

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

68 files changed

+2273
-125
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"problem_name": "maximum_subarray",
3+
"solution_class_name": "Solution",
4+
"problem_number": "53",
5+
"problem_title": "Maximum Subarray",
6+
"difficulty": "Medium",
7+
"topics": "Array, Divide and Conquer, Dynamic Programming",
8+
"_tags": { "list": ["grind-75"] },
9+
"readme_description": "Given an integer array `nums`, find the subarray with the largest sum, and return its sum.",
10+
"_readme_examples": {
11+
"list": [
12+
{
13+
"content": "```\nInput: nums = [-2,1,-3,4,-1,2,1,-5,4]\nOutput: 6\n```\n**Explanation:** The subarray [4,-1,2,1] has the largest sum 6."
14+
},
15+
{
16+
"content": "```\nInput: nums = [1]\nOutput: 1\n```\n**Explanation:** The subarray [1] has the largest sum 1."
17+
},
18+
{
19+
"content": "```\nInput: nums = [5,4,-1,7,8]\nOutput: 23\n```\n**Explanation:** The subarray [5,4,-1,7,8] has the largest sum 23."
20+
}
21+
]
22+
},
23+
"readme_constraints": "- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`",
24+
"readme_additional": "**Follow up:** If you have figured out the `O(n)` solution, try coding another solution using the **divide and conquer** approach, which is more subtle.",
25+
"helpers_imports": "",
26+
"helpers_content": "",
27+
"helpers_run_name": "max_sub_array",
28+
"helpers_run_signature": "(solution_class: type, nums: list[int])",
29+
"helpers_run_body": " implementation = solution_class()\n return implementation.max_sub_array(nums)",
30+
"helpers_assert_name": "max_sub_array",
31+
"helpers_assert_signature": "(result: int, expected: int) -> bool",
32+
"helpers_assert_body": " assert result == expected\n return True",
33+
"solution_imports": "",
34+
"solution_contents": "",
35+
"solution_class_content": "",
36+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_max_sub_array, run_max_sub_array\nfrom .solution import Solution",
37+
"test_content": "",
38+
"test_class_name": "MaximumSubarray",
39+
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
40+
"_solution_methods": {
41+
"list": [
42+
{
43+
"name": "max_sub_array",
44+
"signature": "(self, nums: list[int]) -> int",
45+
"body": " # TODO: Implement max_sub_array\n return 0"
46+
}
47+
]
48+
},
49+
"_test_helper_methods": {
50+
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
51+
},
52+
"_test_methods": {
53+
"list": [
54+
{
55+
"name": "test_max_sub_array",
56+
"signature": "(self, nums: list[int], expected: int)",
57+
"parametrize": "nums, expected",
58+
"test_cases": "[([-2, 1, -3, 4, -1, 2, 1, -5, 4], 6), ([1], 1), ([5, 4, -1, 7, 8], 23), ([-1], -1), ([-2, -1], -1), ([1, 2, 3, 4, 5], 15), ([-5, -2, -8, -1], -1)]",
59+
"body": " result = run_max_sub_array(Solution, nums)\n assert_max_sub_array(result, expected)"
60+
}
61+
]
62+
},
63+
"playground_imports": "from helpers import run_max_sub_array, assert_max_sub_array\nfrom solution import Solution",
64+
"playground_setup": "# Example test case\nnums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]\nexpected = 6",
65+
"playground_run": "result = run_max_sub_array(Solution, nums)\nresult",
66+
"playground_assert": "assert_max_sub_array(result, expected)"
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"problem_name": "merge_intervals",
3+
"solution_class_name": "Solution",
4+
"problem_number": "56",
5+
"problem_title": "Merge Intervals",
6+
"difficulty": "Medium",
7+
"topics": "Array, Sorting",
8+
"_tags": { "list": ["grind-75"] },
9+
"readme_description": "Given an array of `intervals` where `intervals[i] = [starti, endi]`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.",
10+
"_readme_examples": {
11+
"list": [
12+
{
13+
"content": "```\nInput: intervals = [[1,3],[2,6],[8,10],[15,18]]\nOutput: [[1,6],[8,10],[15,18]]\n```\n**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6]."
14+
},
15+
{
16+
"content": "```\nInput: intervals = [[1,4],[4,5]]\nOutput: [[1,5]]\n```\n**Explanation:** Intervals [1,4] and [4,5] are considered overlapping."
17+
},
18+
{
19+
"content": "```\nInput: intervals = [[4,7],[1,4]]\nOutput: [[1,7]]\n```\n**Explanation:** Intervals [1,4] and [4,7] are considered overlapping."
20+
}
21+
]
22+
},
23+
"readme_constraints": "- `1 <= intervals.length <= 10^4`\n- `intervals[i].length == 2`\n- `0 <= starti <= endi <= 10^4`",
24+
"readme_additional": "",
25+
"helpers_imports": "",
26+
"helpers_content": "",
27+
"helpers_run_name": "merge",
28+
"helpers_run_signature": "(solution_class: type, intervals: list[list[int]])",
29+
"helpers_run_body": " implementation = solution_class()\n return implementation.merge(intervals)",
30+
"helpers_assert_name": "merge",
31+
"helpers_assert_signature": "(result: list[list[int]], expected: list[list[int]]) -> bool",
32+
"helpers_assert_body": " assert result == expected\n return True",
33+
"solution_imports": "",
34+
"solution_contents": "",
35+
"solution_class_content": "",
36+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_merge, run_merge\nfrom .solution import Solution",
37+
"test_content": "",
38+
"test_class_name": "MergeIntervals",
39+
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
40+
"_solution_methods": {
41+
"list": [
42+
{
43+
"name": "merge",
44+
"signature": "(self, intervals: list[list[int]]) -> list[list[int]]",
45+
"body": " # TODO: Implement merge\n return []"
46+
}
47+
]
48+
},
49+
"_test_helper_methods": {
50+
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
51+
},
52+
"_test_methods": {
53+
"list": [
54+
{
55+
"name": "test_merge",
56+
"signature": "(self, intervals: list[list[int]], expected: list[list[int]])",
57+
"parametrize": "intervals, expected",
58+
"test_cases": "[([[1,3],[2,6],[8,10],[15,18]], [[1,6],[8,10],[15,18]]), ([[1,4],[4,5]], [[1,5]]), ([[4,7],[1,4]], [[1,7]]), ([[1,3]], [[1,3]]), ([[1,4],[2,3]], [[1,4]]), ([[1,2],[3,4],[5,6]], [[1,2],[3,4],[5,6]])]",
59+
"body": " result = run_merge(Solution, intervals)\n assert_merge(result, expected)"
60+
}
61+
]
62+
},
63+
"playground_imports": "from helpers import run_merge, assert_merge\nfrom solution import Solution",
64+
"playground_setup": "# Example test case\nintervals = [[1,3],[2,6],[8,10],[15,18]]\nexpected = [[1,6],[8,10],[15,18]]",
65+
"playground_run": "result = run_merge(Solution, intervals)\nresult",
66+
"playground_assert": "assert_merge(result, expected)"
67+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"problem_name": "merge_k_sorted_lists",
3+
"solution_class_name": "Solution",
4+
"problem_number": "23",
5+
"problem_title": "Merge k Sorted Lists",
6+
"difficulty": "Hard",
7+
"topics": "Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort",
8+
"_tags": { "list": ["grind-75"] },
9+
"readme_description": "You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.\n\n*Merge all the linked-lists into one sorted linked-list and return it.*",
10+
"_readme_examples": {
11+
"list": [
12+
{
13+
"content": "```\nInput: lists = [[1,4,5],[1,3,4],[2,6]]\nOutput: [1,1,2,3,4,4,5,6]\n```\n**Explanation:** The linked-lists are:\n```\n[\n 1->4->5,\n 1->3->4,\n 2->6\n]\n```\nmerging them into one sorted linked list:\n```\n1->1->2->3->4->4->5->6\n```"
14+
},
15+
{ "content": "```\nInput: lists = []\nOutput: []\n```" },
16+
{ "content": "```\nInput: lists = [[]]\nOutput: []\n```" }
17+
]
18+
},
19+
"readme_constraints": "- `k == lists.length`\n- `0 <= k <= 10^4`\n- `0 <= lists[i].length <= 500`\n- `-10^4 <= lists[i][j] <= 10^4`\n- `lists[i]` is sorted in ascending order.\n- The sum of `lists[i].length` will not exceed `10^4`.",
20+
"readme_additional": "",
21+
"helpers_imports": "from leetcode_py import ListNode",
22+
"helpers_content": "",
23+
"helpers_run_name": "merge_k_lists",
24+
"helpers_run_signature": "(solution_class: type, lists_data: list[list[int]])",
25+
"helpers_run_body": " lists = [ListNode[int].from_list(lst) for lst in lists_data]\n implementation = solution_class()\n return implementation.merge_k_lists(lists)",
26+
"helpers_assert_name": "merge_k_lists",
27+
"helpers_assert_signature": "(result: ListNode[int] | None, expected_data: list[int]) -> bool",
28+
"helpers_assert_body": " expected = ListNode[int].from_list(expected_data)\n assert result == expected\n return True",
29+
"solution_imports": "from leetcode_py import ListNode",
30+
"solution_contents": "",
31+
"solution_class_content": "",
32+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_merge_k_lists, run_merge_k_lists\nfrom .solution import Solution",
33+
"test_content": "",
34+
"test_class_name": "MergeKSortedLists",
35+
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
36+
"_solution_methods": {
37+
"list": [
38+
{
39+
"name": "merge_k_lists",
40+
"signature": "(self, lists: list[ListNode[int] | None]) -> ListNode[int] | None",
41+
"body": " # TODO: Implement merge_k_lists\n return None"
42+
}
43+
]
44+
},
45+
"_test_helper_methods": {
46+
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
47+
},
48+
"_test_methods": {
49+
"list": [
50+
{
51+
"name": "test_merge_k_lists",
52+
"signature": "(self, lists_data: list[list[int]], expected_data: list[int])",
53+
"parametrize": "lists_data, expected_data",
54+
"test_cases": "[([[1, 4, 5], [1, 3, 4], [2, 6]], [1, 1, 2, 3, 4, 4, 5, 6]), ([], []), ([[]], []), ([[1]], [1]), ([[1, 2], [3, 4]], [1, 2, 3, 4]), ([[5], [1, 3], [2, 4, 6]], [1, 2, 3, 4, 5, 6]), ([[-1, 0, 1], [-2, 2]], [-2, -1, 0, 1, 2]), ([[1, 1, 1], [2, 2, 2]], [1, 1, 1, 2, 2, 2]), ([[], [1], []], [1])]",
55+
"body": " result = run_merge_k_lists(Solution, lists_data)\n assert_merge_k_lists(result, expected_data)"
56+
}
57+
]
58+
},
59+
"playground_imports": "from helpers import run_merge_k_lists, assert_merge_k_lists\nfrom solution import Solution\nfrom leetcode_py import ListNode",
60+
"playground_setup": "# Example test case\nlists_data = [[1, 4, 5], [1, 3, 4], [2, 6]]\nexpected_data = [1, 1, 2, 3, 4, 4, 5, 6]",
61+
"playground_run": "result = run_merge_k_lists(Solution, lists_data)\nListNode[int].to_list(result) if result else []",
62+
"playground_assert": "assert_merge_k_lists(result, expected_data)"
63+
}

.templates/leetcode/json/merge_two_sorted_lists.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151
"name": "test_merge_two_lists",
5252
"signature": "(self, list1_vals: list[int], list2_vals: list[int], expected_vals: list[int])",
5353
"parametrize": "list1_vals, list2_vals, expected_vals",
54-
"test_cases": "[([1, 2, 4], [1, 3, 4], [1, 1, 2, 3, 4, 4]), ([], [], []), ([], [0], [0]), ([0], [], [0]), ([1], [2], [1, 2]), ([2], [1], [1, 2]), ([1, 1, 1], [2, 2, 2], [1, 1, 1, 2, 2, 2]), ([1, 3, 5], [2, 4, 6], [1, 2, 3, 4, 5, 6]), ([-10, -5, 0], [-8, -3, 1], [-10, -8, -5, -3, 0, 1]), ([5], [1, 2, 3, 4], [1, 2, 3, 4, 5]), ([1, 2, 3, 4], [5], [1, 2, 3, 4, 5])]",
54+
"test_cases": "[([1, 2, 4], [1, 3, 4], [1, 1, 2, 3, 4, 4]), ([], [], []), ([], [0], [0]), ([1], [2], [1, 2]), ([2], [1], [1, 2]), ([1, 3, 5], [2, 4, 6], [1, 2, 3, 4, 5, 6]), ([1, 1, 1], [2, 2, 2], [1, 1, 1, 2, 2, 2])]",
5555
"body": " result = run_merge_two_lists(Solution, list1_vals, list2_vals)\n assert_merge_two_lists(result, expected_vals)"
5656
}
5757
]
5858
},
59-
"playground_imports": "from helpers import run_merge_two_lists, assert_merge_two_lists\nfrom solution import Solution",
59+
"playground_imports": "from helpers import run_merge_two_lists, assert_merge_two_lists\nfrom solution import Solution\nfrom leetcode_py import ListNode",
6060
"playground_setup": "# Example test case\nlist1_vals = [1, 2, 4]\nlist2_vals = [1, 3, 4]\nexpected_vals = [1, 1, 2, 3, 4, 4]",
61-
"playground_run": "result = run_merge_two_lists(Solution, list1_vals, list2_vals)\nresult",
61+
"playground_run": "result = run_merge_two_lists(Solution, list1_vals, list2_vals)\nListNode[int].to_list(result) if result else []",
6262
"playground_assert": "assert_merge_two_lists(result, expected_vals)"
6363
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"problem_name": "middle_of_the_linked_list",
3+
"solution_class_name": "Solution",
4+
"problem_number": "876",
5+
"problem_title": "Middle of the Linked List",
6+
"difficulty": "Easy",
7+
"topics": "Linked List, Two Pointers",
8+
"_tags": { "list": ["grind-75"] },
9+
"readme_description": "Given the `head` of a singly linked list, return *the middle node of the linked list*.\n\nIf there are two middle nodes, return **the second middle** node.",
10+
"_readme_examples": {
11+
"list": [
12+
{
13+
"content": "![Example 1](https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg)\n\n```\nInput: head = [1,2,3,4,5]\nOutput: [3,4,5]\n```\n**Explanation:** The middle node of the list is node 3."
14+
},
15+
{
16+
"content": "![Example 2](https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg)\n\n```\nInput: head = [1,2,3,4,5,6]\nOutput: [4,5,6]\n```\n**Explanation:** Since the list has two middle nodes with values 3 and 4, we return the second one."
17+
}
18+
]
19+
},
20+
"readme_constraints": "- The number of nodes in the list is in the range `[1, 100]`.\n- `1 <= Node.val <= 100`",
21+
"readme_additional": "",
22+
"helpers_imports": "from leetcode_py import ListNode",
23+
"helpers_content": "",
24+
"helpers_run_name": "middle_node",
25+
"helpers_run_signature": "(solution_class: type, head_list: list[int])",
26+
"helpers_run_body": " head = ListNode[int].from_list(head_list)\n implementation = solution_class()\n return implementation.middle_node(head)",
27+
"helpers_assert_name": "middle_node",
28+
"helpers_assert_signature": "(result: ListNode[int] | None, expected_list: list[int]) -> bool",
29+
"helpers_assert_body": " expected = ListNode[int].from_list(expected_list)\n assert result == expected\n return True",
30+
"solution_imports": "from leetcode_py import ListNode",
31+
"solution_contents": "",
32+
"solution_class_content": "",
33+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_middle_node, run_middle_node\nfrom .solution import Solution",
34+
"test_content": "",
35+
"test_class_name": "MiddleOfTheLinkedList",
36+
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
37+
"_solution_methods": {
38+
"list": [
39+
{
40+
"name": "middle_node",
41+
"signature": "(self, head: ListNode[int] | None) -> ListNode[int] | None",
42+
"body": " # TODO: Implement middle_node\n return None"
43+
}
44+
]
45+
},
46+
"_test_helper_methods": {
47+
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
48+
},
49+
"_test_methods": {
50+
"list": [
51+
{
52+
"name": "test_middle_node",
53+
"signature": "(self, head_list: list[int], expected_list: list[int])",
54+
"parametrize": "head_list, expected_list",
55+
"test_cases": "[([1, 2, 3, 4, 5], [3, 4, 5]), ([1, 2, 3, 4, 5, 6], [4, 5, 6]), ([1], [1]), ([1, 2], [2]), ([1, 2, 3], [2, 3]), ([1, 2, 3, 4], [3, 4]), ([10, 20, 30, 40, 50, 60, 70], [40, 50, 60, 70])]",
56+
"body": " result = run_middle_node(Solution, head_list)\n assert_middle_node(result, expected_list)"
57+
}
58+
]
59+
},
60+
"playground_imports": "from helpers import run_middle_node, assert_middle_node\nfrom solution import Solution\nfrom leetcode_py import ListNode",
61+
"playground_setup": "# Example test case\nhead_list = [1, 2, 3, 4, 5]\nexpected_list = [3, 4, 5]",
62+
"playground_run": "result = run_middle_node(Solution, head_list)\nListNode[int].to_list(result) if result else []",
63+
"playground_assert": "assert_middle_node(result, expected_list)"
64+
}

0 commit comments

Comments
 (0)