973. K Closest Points to Origin Medium
1/**
2 * [973] K Closest Points to Origin
3 *
4 * 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).
5 * The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)^2 + (y1 - y2)^2).
6 * You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg" style="width: 400px; height: 400px;" />
10 * Input: points = [[1,3],[-2,2]], k = 1
11 * Output: [[-2,2]]
12 * Explanation:
13 * The distance between (1, 3) and the origin is sqrt(10).
14 * The distance between (-2, 2) and the origin is sqrt(8).
15 * Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
16 * We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
17 *
18 * Example 2:
19 *
20 * Input: points = [[3,3],[5,-1],[-2,4]], k = 2
21 * Output: [[3,3],[-2,4]]
22 * Explanation: The answer [[-2,4],[3,3]] would also be accepted.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= k <= points.length <= 10^4
28 * -10^4 < xi, yi < 10^4
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/k-closest-points-to-origin/
34// discuss: https://leetcode.com/problems/k-closest-points-to-origin/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn k_closest(points: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
40 vec![]
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_973() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.