FB面经 Prepare: K closest point to the origin

Give n points on 2-D plane, find the K closest points to origin
 1 package fbPractise;
 2
 3 import java.util.*;
 4
 5 class Coordinate {
 6     int x;
 7     int y;
 8     public Coordinate(int x, int y) {
 9         this.x = x;
10         this.y = y;
11     }
12 }
13
14 public class Kclosest {
15
16     public static List<Coordinate> findK(List<Coordinate> input, int k) {
17         HashMap<Coordinate, Integer> map = new HashMap<Coordinate, Integer>();
18         int longest = 0;
19         for (Coordinate each : input) {
20             int distance = cal(each);
21             map.put(each, distance);
22             longest = Math.max(longest, distance);
23         }
24
25         List<Coordinate>[] arr = new ArrayList[longest + 1];
26         for (Coordinate each : map.keySet()) {
27             int dis = map.get(each);
28             if (arr[dis] == null)
29                 arr[dis] = new ArrayList<Coordinate>();
30             arr[dis].add(each);
31         }
32
33         List<Coordinate> res = new ArrayList<Coordinate>();
34         for (int i=0; i<arr.length-1 && res.size()<k; i++) {
35             if (arr[i] != null)
36                 res.addAll(arr[i]);
37         }
38         return res;
39     }
40
41     public static int cal(Coordinate a) {
42         return a.x * a.x + a.y * a.y;
43     }
44
45
46     /**
47      * @param args
48      */
49     public static void main(String[] args) {
50         // TODO Auto-generated method stub
51         Coordinate c1 = new Coordinate(1,2);
52         Coordinate c2 = new Coordinate(1,3);
53         Coordinate c3 = new Coordinate(2,5);
54         List<Coordinate> list = new ArrayList<Coordinate>();
55         list.add(c1);
56         list.add(c2);
57         list.add(c3);
58         List<Coordinate> res = findK(list, 2);
59         for (Coordinate each : res) {
60             System.out.print(each.x);
61             System.out.println(each.y);
62         }
63     }
64
65 }
时间: 2024-10-11 00:27:48

FB面经 Prepare: K closest point to the origin的相关文章

Microsoft - Find the K closest points to the origin in a 2D plane

Find the K closest points to the origin in a 2D plane, given an array containing N points. 用 max heap 做 /* public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } */ public List<Point> findKClosest(P

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

LC 973. K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

[Solution] 973. K Closest Points to Origin

Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order. The answer is guaran

LeetCode 973. K Closest Points to Origin

原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return

973. K Closest Points to Origin - Medium

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

K closest points

Find the K closest points to a target point in a 2D plane. import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; class Point { public int x; public int y; public Point(int x, int y) { this.x =

K Closest Numbers In Sorted Array

Given a target number, a non-negative integer k and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending ord

K Closest Points Lintcode

Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.Return these points sorted by distance, if they are same with distance, sorted by x-axis, otherwise sorted by y-axis. Exam