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 ...
随机推荐
- Concept of function continuity in topology
Understanding of continuity definition in topology When we learn calculus in university as freshmen, ...
- spark Transformations算子
在java中,RDD分为javaRDDs和javaPairRDDs.下面分两大类来进行. 都必须要进行的一步. SparkConf conf = new SparkConf().setMaster(& ...
- 原生HttpClient详细使用示例
一.HttpClient类 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; ...
- 微信小程序--家庭记账本开发--02
代码文件的了解 对于一个程序的开发,代码的了解的必须的,不同的后缀名文件有着不同的作用,通过学习,对于微信小程序开发中的文件有了自己的理解. 文件概述 一个微信小程序一般有有pages文件夹,util ...
- 【C#】时间类型修改
鉴于前后端分离发展的迅速.前端很多时间控件都会读UTC时间. 安利一个小知识 // // 摘要: // Creates a new System.DateTime object that has th ...
- c++中一个多态的实例
#include <iostream> #include <fstream> #include <vector> #include <algorithm> ...
- 02-Python入门学习-变量
一.编程语言介绍1.机器语言:直接用二进制编程,直接控制硬件,需要掌握硬件的操作细节优点:执行效率高缺点:开发效率低 2.汇编语言:用英文标签取代二进制指令去编写程序,直接控制硬件,需要掌握硬件的操作 ...
- Urozero Autumn 2016. BAPC 2016
A. Airport Logistics 根据光路最快原理以及斯涅尔定律,可以得到从定点$P$进入某条直线的最佳入射角. 求出每个端点到每条线段的最佳点,建图求最短路即可. 时间复杂度$O(n^2\l ...
- swift中单例类的创建
1.最经典的 class TheOneAndOnlyKraken { static let sharedInstance = TheOneAndOnlyKraken() private ...
- (56)Wangdao.com第八天_JavaScript 流程控制语句
流程控制语句 条件判断语句 if 条件分支语句 switch 循环语句 for .while switch 和 if 都可以相互转换,switch 的性能更优于 if 1. 条件判断语句 if 在某条 ...