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. LeetCode 709.To Lower Case

    Description Implement function ToLowerCase() that has a string parameter str, and returns the same s ...

  2. asp.net core的DI框架思考以及服务实例的获取方式总结

    转载请注明出处: https://home.cnblogs.com/u/zhiyong-ITNote/ 整个asp.net core管道从WebHostBuilder到WebHost到后续请求的类中, ...

  3. CentOS6.5安装图形用户界面

    CentOS 6.5 安装图形界面 安装的时候没有安装图像界面.安装步骤如下: 1.yum -y groupinstall Desktop 2.yum -y groupinstall "X ...

  4. Python开发技术详解PDF

    Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...

  5. MySQL性能分析及explain的使用(转)

    1.使用explain语句去查看分析结果,如 explain select * from test1 where id=1; 会出现: id selecttype table type possibl ...

  6. __x__(19)0907第四天__ HTML5 文本标签 及 样式

    strong 表语义上的强调, em 表示语气上的强调: <strong>警告:离僵尸远点!</strong> 世界末日了,因为僵尸是<em>有毒的</em& ...

  7. CSS3_盒子背景

    盒子背景 盒子背景:content    padding    特殊的 boder 背景 背景绘制 从 padding 开始绘制 背景裁剪 background-clip(默认值 border-box ...

  8. javaweb中的乱码问题(初次接触时写)

    javaweb中的乱码问题 在初次接触javaweb中就遇到了乱码问题,下面是我遇到这些问题的解决办法 1. 页面乱码(jsp) 1. 在页面最前方加上 <%@ page language=&q ...

  9. Windows系统maven安装配置

    Apache Maven是一个软件项目管理工具,基于项目对象模型(Project Object Model,即POM)的概念,Maven可用来管理项目的依赖.编译.文档等信息.使用Maven管理项目时 ...

  10. js根据服务端返回的时间倒计时

    使用服务端与本地的时间差进行计算 $(function(){ // 倒计时 var _ordertimer = null; var data =new Date(); var txt = $('.js ...