Give n points on 2-D plane, find the K closest points to origin

Based on bucket sort:

 package fbPractise;

 import java.util.*;

 class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
} public class Kclosest { public static List<Coordinate> findK(List<Coordinate> input, int k) {
HashMap<Coordinate, Integer> map = new HashMap<Coordinate, Integer>();
int longest = 0;
for (Coordinate each : input) {
int distance = cal(each);
map.put(each, distance);
longest = Math.max(longest, distance);
} List<Coordinate>[] arr = new ArrayList[longest + 1];
for (Coordinate each : map.keySet()) {
int dis = map.get(each);
if (arr[dis] == null)
arr[dis] = new ArrayList<Coordinate>();
arr[dis].add(each);
} List<Coordinate> res = new ArrayList<Coordinate>();
for (int i=0; i<arr.length-1 && res.size()<k; i++) {
if (arr[i] != null)
res.addAll(arr[i]);
}
return res;
} public static int cal(Coordinate a) {
return a.x * a.x + a.y * a.y;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Coordinate c1 = new Coordinate(1,2);
Coordinate c2 = new Coordinate(1,3);
Coordinate c3 = new Coordinate(2,5);
List<Coordinate> list = new ArrayList<Coordinate>();
list.add(c1);
list.add(c2);
list.add(c3);
List<Coordinate> res = findK(list, 2);
for (Coordinate each : res) {
System.out.print(each.x);
System.out.println(each.y);
}
} }

Based on Quick Select

 package fbOnsite;

 import java.util.*;

 class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
} public class ClosestKPoints { public static List<Coordinate> findK(List<Coordinate> input, int k, Coordinate target) {
HashMap<Coordinate, Integer> map = new HashMap<Coordinate, Integer>();
for (Coordinate each : input) {
int distance = cal(each, target);
map.put(each, distance);
}
List<Coordinate> res = help(input, 0, input.size()-1, k, map);
return res;
} public static List<Coordinate> help(List<Coordinate> input, int start, int end, int k, HashMap<Coordinate, Integer> map) {
List<Coordinate> res = new ArrayList<Coordinate>();
int l = start, r = end;
int pivot = r;
while (l < r) {
while (l<r && map.get(input.get(l))<map.get(input.get(pivot))) l++;
while (l<r && map.get(input.get(r))>=map.get(input.get(pivot))) r--;
if (l >= r) break;
swap(input, l, r);
}
swap(input, l, pivot);
if (l+1 == k) {
for (int i=0; i<=l; i++) {
res.add(input.get(i));
}
return res;
}
else if (l+1 < k) {
return help(input, l+1, end, k, map);
}
else return help(input, start, l-1, k, map);
} public static int cal(Coordinate a, Coordinate target) {
return (a.x-target.x)*(a.x-target.x) + (a.y-target.y)*(a.y-target.y);
} public static void swap(List<Coordinate> input, int l, int r) {
Coordinate temp = input.get(l);
input.set(l, input.get(r));
input.set(r, temp);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Coordinate c1 = new Coordinate(1,2);
Coordinate c2 = new Coordinate(1,3);
Coordinate c3 = new Coordinate(2,5);
List<Coordinate> list = new ArrayList<Coordinate>();
list.add(c1);
list.add(c2);
list.add(c3);
List<Coordinate> res = findK(list, 2, new Coordinate(2,6));
for (Coordinate each : res) {
System.out.print(each.x);
System.out.println(each.y);
}
} }

当然,还有的方法是维护一个size为k的最大堆

 package fbOnsite;
import java.util.*;
public class Kpoints {
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
} public List<Point> KClosest(List<Point> input, int k) {
List<Point> res = new LinkedList<Point>();
PriorityQueue<Point> pq = new PriorityQueue<Point>(10, new Comparator<Point>() {
public int compare(Point a, Point b) {
return (b.x * b.x + b.y * b.y) - (a.x * a.x + a.y * a.y);
}
}); for (Point each : input) {
pq.offer(each);
if (pq.size() > k) pq.poll();
}
while (!pq.isEmpty()) {
res.add(0, pq.poll());
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Kpoints sol = new Kpoints();
List<Point> input = new ArrayList<Point>();
input.add(sol.new Point(1,2));
input.add(sol.new Point(3,5));
input.add(sol.new Point(2,3));
input.add(sol.new Point(-1,-7));
input.add(sol.new Point(-1,-2));
List<Point> res = sol.KClosest(input, 3);
for (Point each : res) {
System.out.print(each.x);
System.out.println(each.y);
}
} }

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

  1. 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 hea ...

  2. [Swift]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 d ...

  3. [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 ori ...

  4. LeetCode 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, ...

  5. LeetCode 973. K Closest Points to Origin

    原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...

  6. 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 d ...

  7. 119th LeetCode Weekly Contest 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 d ...

  8. 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 d ...

  9. 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 d ...

随机推荐

  1. 模块度Q

    模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...

  2. AtCoder Grand Contest 027 (AGC017) D - Modulo Matrix 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC027C.html 题解 首先我们假装 max mod min = 1 然后对着这个构造. 将各自黑白染色, ...

  3. mysql命令之工作小结

    1.登客户端 mysql   -u userName  -p password   -h  ip    注:u 用户名   p 密码   h  ip地址 2.修改密码 UPDATE   mysql.u ...

  4. 业务线--node中间层做一个透传的项目

    1,node中间层总结 1,ejs引入vue的js,路由层(直接透传,自定义行的),比较浅层的一层 中间件的引入 ? ) { // 与rd约定,接口成功返回code===0,其余为失败 console ...

  5. (三)ajax请求不同源之nginx反向代理跨域

    一.基本原理 nginx是一个高性能的web服务器,常用作反向代理服务器.nginx作为反向代理服务器,就是把http请求转发到另一个或者一些服务器上. 用nginx反向代理实现跨域,是最简单的跨域方 ...

  6. vscode断点调试工程化服务端文件

    一.创建express应用我们使用express-generator创建一个新的express应用.1.全局安装express-generator // 安装 sudo npm install exp ...

  7. CodeForces - 1025C 字符串处理,画一个圆。。。

    题目链接: https://vjudge.net/problem/1810469/origin 题目大意: 给你一个字符串,中间切一刀,左右两边均反转,然后右边的串拼接到左边上. 思路: 比如  aa ...

  8. 错误解决记录-------------验证启动HDFS时遇到的错误

    主要解决验证启动HDFS时: 1) jps:bash: jps: command not found... 原因:主要是java/bin 环境变量没配置好. 解决办法: 在  ~/.bash_prof ...

  9. python3类方法与静态方法

    静态⽅法和类⽅法 转载于:https://blog.csdn.net/qq_41020281/article/details/79634707 1. 类⽅法 是类对象所拥有的⽅法,需要⽤修饰器 @cl ...

  10. h5 canvas与SVG的比较

    画布 什么是canvas? HTML5的canvas标签使用JavaScript可以在网页上绘制图像,画布为一个矩形. 画布本身没有绘制能力,只能通过脚本来绘制. 画布例子: <canvas i ...