FB面经 Prepare: K closest point to the origin
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 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, ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python之基于libsvm识别数字验证码
1. 参考 字符型图片验证码识别完整过程及Python实现 2.图片预处理和手动分类 (1)分析图片 from PIL import Image img = Image.open('nums/ttt. ...
- Java设计模式之建造者模式(生成器模式)
建造者模式: 也叫生成器模式.用来隐藏复合对象的创建过程,他把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动态地创建具有复合属性的对象. 总结一句就是封装一个对象的构造过程,并允许按步骤构造 ...
- 命令行编译C程序
1 准备工作 下载mingw-get-setup.exe并且安装 参考 http://www.jb51.net/softjc/159871.html 环境变量更新: PATH .;C:\MinGW\ ...
- Orleans框架简单示范
希望现在学习Orleans不会晚,毕竟Server Fabric都开源了.本篇博客从Sample的HelloWorld示例程序来解读Orleans的Grains. Server配置参考 : https ...
- A network-related or instance-specific error occurred while establishing a connection to SQL Server
今天同事给我发了个图片过来, 服务器环境 sql 2000 + IIS7 看到这张图片,我先自己试了下,确实是有这个问题的,而且不是偶然性的,那么再看报错意思,在跟sql建立连接的时候发生了一个错误 ...
- 使用javascript和css模拟帧动画的几种方法浅析
我们平时在开发前端页面的时候,经常会播放一段帧序列.这段帧序列就像gif图片那样,反复循环播放.那大家可能会说,直接用gif图片就好了,干嘛还去模拟呢?那是因为要做得更加灵活,我们要做到以下几点: 1 ...
- ajax 传递中文字符参数 问题
使用ajax 传递中文字符串时, 服务端会接收不到预期的 中文字符. 此时,需要对 js中的中文字符参数进行 编码, 到达服务端后, 再为其解码 即可. 前端: var url = '....'; ...
- puppeteer 填充基础表单
main.js const pptr = require("puppeteer"); const gotoUrl = "http://127.0.0.1:5500/ind ...
- js中函数创建的三种方式
1.函数声明 function sum1(n1,n2){ return n1+n2; }; 2.函数表达式,又叫函数字面量 var sum2=function(n1,n2){ re ...
- oracle 内存不足处理
alter日志 TNS-12535: TNS:operation timed out ns secondary err code: 12606 nt main err code: 0 nt secon ...