|
| 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": "\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 | +} |
0 commit comments