Skip to content

Commit cc0ad21

Browse files
committed
feat: add more problems
1 parent 09637c7 commit cc0ad21

File tree

23 files changed

+598
-5
lines changed

23 files changed

+598
-5
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"problem_name": "k_closest_points_to_origin",
3+
"solution_class_name": "Solution",
4+
"problem_number": "973",
5+
"problem_title": "K Closest Points to Origin",
6+
"difficulty": "Medium",
7+
"topics": "Array, Math, Divide and Conquer, Geometry, Sorting, Heap (Priority Queue), Quickselect",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an array of `points` where `points[i] = [xi, yi]` represents a point on the **X-Y** plane and an integer `k`, return the `k` closest points to the origin `(0, 0)`.\n\nThe distance between two points on the **X-Y** plane is the Euclidean distance (i.e., `\u221a(x1 - x2)\u00b2 + (y1 - y2)\u00b2`).\n\nYou may return the answer in **any order**. The answer is **guaranteed** to be **unique** (except for the order that it is in).",
10+
"readme_examples": [
11+
{
12+
"content": "![Example 1](https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg)\n\n```\nInput: points = [[1,3],[-2,2]], k = 1\nOutput: [[-2,2]]\n```\n**Explanation:** The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]."
13+
},
14+
{
15+
"content": "```\nInput: points = [[3,3],[5,-1],[-2,4]], k = 2\nOutput: [[3,3],[-2,4]]\n```\n**Explanation:** The answer [[-2,4],[3,3]] would also be accepted."
16+
}
17+
],
18+
"readme_constraints": "- `1 <= k <= points.length <= 10^4`\n- `-10^4 <= xi, yi <= 10^4`",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{
23+
"name": "k_closest",
24+
"parameters": "points: list[list[int]], k: int",
25+
"return_type": "list[list[int]]",
26+
"dummy_return": "[]"
27+
}
28+
],
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
30+
"test_class_name": "KClosestPointsToOrigin",
31+
"test_helper_methods": [
32+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
33+
],
34+
"test_methods": [
35+
{
36+
"name": "test_k_closest",
37+
"parametrize": "points, k, expected",
38+
"parametrize_typed": "points: list[list[int]], k: int, expected: list[list[int]]",
39+
"test_cases": "[([[1, 3], [-2, 2]], 1, [[-2, 2]]), ([[3, 3], [5, -1], [-2, 4]], 2, [[3, 3], [-2, 4]]), ([[0, 1], [1, 0]], 2, [[0, 1], [1, 0]]), ([[1, 1], [1, 1], [1, 1]], 2, [[1, 1], [1, 1]])]",
40+
"body": "result = self.solution.k_closest(points, k)\n# Sort both result and expected for comparison since order doesn't matter\nresult_sorted = sorted(result)\nexpected_sorted = sorted(expected)\nassert result_sorted == expected_sorted"
41+
}
42+
],
43+
"playground_imports": "from solution import Solution",
44+
"playground_test_case": "# Example test case\npoints = [[1, 3], [-2, 2]]\nk = 1\nexpected = [[-2, 2]]",
45+
"playground_execution": "result = Solution().k_closest(points, k)\nresult",
46+
"playground_assertion": "assert sorted(result) == sorted(expected)"
47+
}

.templates/leetcode/json/linked_list_cycle.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"problem_title": "Linked List Cycle",
66
"difficulty": "Easy",
77
"topics": "Hash Table, Linked List, Two Pointers",
8-
"tags": [],
8+
"tags": ["grind-75"],
99
"readme_description": "Given `head`, the head of a linked list, determine if the linked list has a cycle in it.\n\nThere is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to. **Note that `pos` is not passed as a parameter**.\n\nReturn `true` *if there is a cycle in the linked list*. Otherwise, return `false`.",
1010
"readme_examples": [
1111
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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": ["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+
{
12+
"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."
13+
},
14+
{
15+
"content": "```\nInput: nums = [1]\nOutput: 1\n```\n**Explanation:** The subarray [1] has the largest sum 1."
16+
},
17+
{
18+
"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."
19+
}
20+
],
21+
"readme_constraints": "- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`",
22+
"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.",
23+
"solution_imports": "",
24+
"solution_methods": [
25+
{
26+
"name": "max_sub_array",
27+
"parameters": "nums: list[int]",
28+
"return_type": "int",
29+
"dummy_return": "0"
30+
}
31+
],
32+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
33+
"test_class_name": "MaximumSubarray",
34+
"test_helper_methods": [
35+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
36+
],
37+
"test_methods": [
38+
{
39+
"name": "test_max_sub_array",
40+
"parametrize": "nums, expected",
41+
"parametrize_typed": "nums: list[int], expected: int",
42+
"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)]",
43+
"body": "result = self.solution.max_sub_array(nums)\nassert result == expected"
44+
}
45+
],
46+
"playground_imports": "from solution import Solution",
47+
"playground_test_case": "# Example test case\nnums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]\nexpected = 6",
48+
"playground_execution": "result = Solution().max_sub_array(nums)\nresult",
49+
"playground_assertion": "assert result == expected"
50+
}

.templates/leetcode/json/reverse_linked_list_ii.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"problem_title": "Reverse Linked List II",
66
"difficulty": "Medium",
77
"topics": "Linked List",
8-
"tags": ["grind-75"],
8+
"tags": [],
99
"readme_description": "Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return the reversed list.",
1010
"readme_examples": [
1111
{ "content": "```\nInput: head = [1,2,3,4,5], left = 2, right = 4\nOutput: [1,4,3,2,5]\n```" },
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"problem_name": "valid_palindrome",
3+
"solution_class_name": "Solution",
4+
"problem_number": "125",
5+
"problem_title": "Valid Palindrome",
6+
"difficulty": "Easy",
7+
"topics": "Two Pointers, String",
8+
"tags": ["grind-75"],
9+
"readme_description": "A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.\n\nGiven a string `s`, return `true` if it is a **palindrome**, or `false` otherwise.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: s = \"A man, a plan, a canal: Panama\"\nOutput: true\n```\n**Explanation:** \"amanaplanacanalpanama\" is a palindrome."
13+
},
14+
{
15+
"content": "```\nInput: s = \"race a car\"\nOutput: false\n```\n**Explanation:** \"raceacar\" is not a palindrome."
16+
},
17+
{
18+
"content": "```\nInput: s = \" \"\nOutput: true\n```\n**Explanation:** s is an empty string \"\" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome."
19+
}
20+
],
21+
"readme_constraints": "- `1 <= s.length <= 2 * 10^5`\n- `s` consists only of printable ASCII characters.",
22+
"readme_additional": "",
23+
"solution_imports": "",
24+
"solution_methods": [
25+
{
26+
"name": "is_palindrome",
27+
"parameters": "s: str",
28+
"return_type": "bool",
29+
"dummy_return": "False"
30+
}
31+
],
32+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
33+
"test_class_name": "ValidPalindrome",
34+
"test_helper_methods": [
35+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
36+
],
37+
"test_methods": [
38+
{
39+
"name": "test_is_palindrome",
40+
"parametrize": "s, expected",
41+
"parametrize_typed": "s: str, expected: bool",
42+
"test_cases": "[(\"A man, a plan, a canal: Panama\", True), (\"race a car\", False), (\" \", True), (\"\", True), (\"a\", True), (\"Madam\", True), (\"No 'x' in Nixon\", True), (\"Mr. Owl ate my metal worm\", True)]",
43+
"body": "result = self.solution.is_palindrome(s)\nassert result == expected"
44+
}
45+
],
46+
"playground_imports": "from solution import Solution",
47+
"playground_test_case": "# Example test case\ns = \"A man, a plan, a canal: Panama\"\nexpected = True",
48+
"playground_execution": "result = Solution().is_palindrome(s)\nresult",
49+
"playground_assertion": "assert result == expected"
50+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= linked_list_cycle
2+
PROBLEM ?= k_closest_points_to_origin
33
FORCE ?= 0
44

55
sync_submodules:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# K Closest Points to Origin
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Math, Divide and Conquer, Geometry, Sorting, Heap (Priority Queue), Quickselect
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 973](https://leetcode.com/problems/k-closest-points-to-origin/description/)
8+
9+
## Problem Description
10+
11+
Given an array of `points` where `points[i] = [xi, yi]` represents a point on the **X-Y** plane and an integer `k`, return the `k` closest points to the origin `(0, 0)`.
12+
13+
The distance between two points on the **X-Y** plane is the Euclidean distance (i.e., `√(x1 - x2)² + (y1 - y2)²`).
14+
15+
You may return the answer in **any order**. The answer is **guaranteed** to be **unique** (except for the order that it is in).
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
![Example 1](https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg)
22+
23+
```
24+
Input: points = [[1,3],[-2,2]], k = 1
25+
Output: [[-2,2]]
26+
```
27+
28+
**Explanation:** The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
29+
30+
### Example 2:
31+
32+
```
33+
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
34+
Output: [[3,3],[-2,4]]
35+
```
36+
37+
**Explanation:** The answer [[-2,4],[3,3]] would also be accepted.
38+
39+
## Constraints
40+
41+
- `1 <= k <= points.length <= 10^4`
42+
- `-10^4 <= xi, yi <= 10^4`

leetcode/k_closest_points_to_origin/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": ["from solution import Solution"]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "setup",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": ["# Example test case\npoints = [[1, 3], [-2, 2]]\nk = 1\nexpected = [[-2, 2]]"]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "execute",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": ["result = Solution().k_closest(points, k)\nresult"]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "test",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": ["assert sorted(result) == sorted(expected)"]
34+
}
35+
],
36+
"metadata": {
37+
"kernelspec": {
38+
"display_name": "leetcode-py-py3.13",
39+
"language": "python",
40+
"name": "python3"
41+
},
42+
"language_info": {
43+
"codemirror_mode": {
44+
"name": "ipython",
45+
"version": 3
46+
},
47+
"file_extension": ".py",
48+
"mimetype": "text/x-python",
49+
"name": "python",
50+
"nbconvert_exporter": "python3",
51+
"pygments_lexer": "ipython3",
52+
"version": "3.13.7"
53+
}
54+
},
55+
"nbformat": 4,
56+
"nbformat_minor": 5
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import heapq
2+
3+
4+
class Solution:
5+
# Time: O(n log k)
6+
# Space: O(k)
7+
def k_closest(self, points: list[list[int]], k: int) -> list[list[int]]:
8+
heap: list[tuple[int, list[int]]] = []
9+
10+
for x, y in points:
11+
dist = x * x + y * y
12+
heapq.heappush(heap, (-dist, [x, y]))
13+
if len(heap) > k:
14+
heapq.heappop(heap)
15+
16+
return [point for _, point in heap]

0 commit comments

Comments
 (0)