博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FB面经 Prepare: K closest point to the origin
阅读量:4501 次
发布时间:2019-06-08

本文共 4593 字,大约阅读时间需要 15 分钟。

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

 Based on bucket sort:

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
findK(List
input, int k) {17 HashMap
map = new HashMap
();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
[] 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
();30 arr[dis].add(each);31 }32 33 List
res = new ArrayList
();34 for (int i=0; i
list = new ArrayList
();55 list.add(c1);56 list.add(c2);57 list.add(c3);58 List
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 }

Based on Quick Select

1 package fbOnsite; 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 ClosestKPoints {15     16     public static List
findK(List
input, int k, Coordinate target) {17 HashMap
map = new HashMap
();18 for (Coordinate each : input) {19 int distance = cal(each, target);20 map.put(each, distance);21 }22 List
res = help(input, 0, input.size()-1, k, map);23 return res;24 }25 26 public static List
help(List
input, int start, int end, int k, HashMap
map) {27 List
res = new ArrayList
();28 int l = start, r = end;29 int pivot = r;30 while (l < r) {31 while (l
=map.get(input.get(pivot))) r--;33 if (l >= r) break;34 swap(input, l, r);35 }36 swap(input, l, pivot);37 if (l+1 == k) {38 for (int i=0; i<=l; i++) {39 res.add(input.get(i));40 }41 return res;42 }43 else if (l+1 < k) {44 return help(input, l+1, end, k, map);45 }46 else return help(input, start, l-1, k, map);47 }48 49 public static int cal(Coordinate a, Coordinate target) {50 return (a.x-target.x)*(a.x-target.x) + (a.y-target.y)*(a.y-target.y);51 }52 53 public static void swap(List
input, int l, int r) {54 Coordinate temp = input.get(l);55 input.set(l, input.get(r));56 input.set(r, temp);57 }58 59 60 /**61 * @param args62 */63 public static void main(String[] args) {64 // TODO Auto-generated method stub65 Coordinate c1 = new Coordinate(1,2);66 Coordinate c2 = new Coordinate(1,3);67 Coordinate c3 = new Coordinate(2,5);68 List
list = new ArrayList
();69 list.add(c1);70 list.add(c2);71 list.add(c3);72 List
res = findK(list, 2, new Coordinate(2,6));73 for (Coordinate each : res) {74 System.out.print(each.x);75 System.out.println(each.y);76 }77 }78 79 }

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

1 package fbOnsite; 2 import java.util.*; 3 public class Kpoints { 4     class Point { 5         int x; 6         int y; 7         public Point(int x, int y) { 8             this.x = x; 9             this.y = y;10         }11     }12     13     public List
KClosest(List
input, int k) {14 List
res = new LinkedList
();15 PriorityQueue
pq = new PriorityQueue
(10, new Comparator
() {16 public int compare(Point a, Point b) {17 return (b.x * b.x + b.y * b.y) - (a.x * a.x + a.y * a.y);18 } 19 });20 21 for (Point each : input) {22 pq.offer(each);23 if (pq.size() > k) pq.poll();24 }25 while (!pq.isEmpty()) {26 res.add(0, pq.poll());27 }28 return res;29 }30 31 /**32 * @param args33 */34 public static void main(String[] args) {35 // TODO Auto-generated method stub36 Kpoints sol = new Kpoints();37 List
input = new ArrayList
();38 input.add(sol.new Point(1,2));39 input.add(sol.new Point(3,5));40 input.add(sol.new Point(2,3));41 input.add(sol.new Point(-1,-7));42 input.add(sol.new Point(-1,-2));43 List
res = sol.KClosest(input, 3);44 for (Point each : res) {45 System.out.print(each.x);46 System.out.println(each.y);47 }48 }49 50 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6546969.html

你可能感兴趣的文章
文件的操作
查看>>
网上图书商城项目学习笔记-007登录功能实现
查看>>
关于mysql的级联删除(之前好多人咨询过我)
查看>>
Linux环境下的C/C+基础调试技术2——程序控制
查看>>
wpf动画同步闪烁
查看>>
3.16上午 复习雅思核心词+新单词100个
查看>>
Html5 部分特性
查看>>
前端工具集合记录
查看>>
浅析负载均衡的6种算法,Ngnix的5种算法
查看>>
OpenCV——图像修补
查看>>
网络概述
查看>>
IntelliJ IDEA - 热部署插件JRebel 安装使用教程
查看>>
(转)华为 安卓手机在MAC系统下 ADB 识别
查看>>
Redis的LRU算法
查看>>
高精度算法
查看>>
ABP WebApi 加载错误
查看>>
iTerm的安装以及配置Oh My Zsh
查看>>
windows上的alfred
查看>>
uc/os 上下文切换
查看>>
COJ 2108 Day7-例1
查看>>