Find the K closest points to a target point in a 2D plane.

class Point {
public int x;
public int y; public Point(int x, int y) {
this.x = x;
this.y = y;
}
} class Solution { public List<Point> findKClosest(Point[] points, int k, Point p) { // max heap
PriorityQueue<Point> pq = new PriorityQueue<>(, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
double d1 = distance(a, p);
double d2 = distance(b, p);
if (d1 == d2)
return ;
if (d1 < d2)
return ;
return -;
}
}); for (int i = ; i < points.length; i++) {
pq.offer(points[i]); if (pq.size() > k) {
pq.poll();
}
} List<Point> x = new ArrayList<>();
while (!pq.isEmpty())
x.add(pq.poll()); return x;
} private double distance(Point p1, Point p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
}

K closest points的更多相关文章

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

  2. [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 ...

  3. 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, ...

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

  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. gradle项目中资源文件的相对路径打包处理技巧

    开发java application时,不管是用ant/maven/gradle中的哪种方式来构建,通常最后都会打包成一个可执行的jar包程序,而程序运行所需的一些资源文件(配置文件),比如jdbc. ...

  2. PHP引用(&)使用详解

    初学php关于&引用 官方文档: 1.引用是什么:http://www.php.net/manual/zh/language.references.whatare.php 2.引用做什么:ht ...

  3. scala速成记录1

    选择  Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/  下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...

  4. 基于C/S架构的3D对战网络游戏C++框架 _02系统设计(总体设计、概要设计)

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  5. iOS项目常用效果方法注意点集锦

    移动中隐藏tabBar,静止显示tabbar - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 隐藏tabbar ...

  6. HTML5全局属性和事件详解

    属性: HTML5属性能够赋给标签元素含义和语境,下面的全局属性可用于任何 HTML5 元素. 属性 描述 contenteditable 规定是否允许用户编辑内容. contextmenu 规定元素 ...

  7. Google调用explorer.exe打开本地文件

    给IE浏览器地址栏输个本地文件路径,会自动用explorer.exe打开,这个挺好的,但是IE对jQuery稍微高点的版本不怎么待见,只好自己给Google折腾一个调用explorer的功能----- ...

  8. C++中Reference与Pointer的不同

    Reference与Pointer中直接存储的都是变量的地址, 它们唯一的不同是前者的存储的地址值是只读的, 而后者可以修改. 也就是说Reference不支持以下操作: *a = b 其他语言, 如 ...

  9. Linux--目录结构解释(转)

    / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存储普通用户的个人文件 ftp --- 用户所有服务 httpd samba user ...

  10. New blog

    New blog //域名还没备案_(:з」∠)_