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. Linux 进程间通讯详解四

    msgsnd函数 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); --功能:把一条消息添加到消息队列中 --参数 ...

  2. C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母

    汉字转拼音貌似一直是C#开发的一个难题,无论什么方案都有一定的bug,之前使用了两种方案. 1.Chinese2Spell.cs 一些不能识别的汉字全部转为Z 2.Microsoft Visual S ...

  3. 万能的林萧说:一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础”。

    来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日,LZ在群里发话,"招人啦." 然某群友曰,"群主,俺想去." LZ回之,"你 ...

  4. 初次尝试用Kotlin实现Android项目

    Kotlin: The Swift of Android 起这个文内标题的原因很简单,就是对Kotlin抱有希望--能使Android的开发更简洁.高效及安全.知道Kotlin是从简书的一篇短文,越来 ...

  5. 【JavaScript】javascript 方法 test()

    个人理解:var b = x.test(y); y是否存在模式x中,返回true或false:x可以是正则,字符串,

  6. Linux 命令积累

    1, su root 切换到root用户 su user 切换到普通用户 2, mkdir / touch 创建文件夹 /文件 3, vi 打开编辑文件 按insert进入编辑模式 编辑完成后 按es ...

  7. 【caffe】绘制网络结构图

    @tags caffe 网络结构 可视化 当拿到一份网络定义文件net.prototxt,可以用工具画出网络结构.最快速的方法是使用在线工具netscope,粘贴内容后shift+回车就可以看结果了. ...

  8. bzoj3527: [Zjoi2014]力

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. windows下MySQL 忘记初始密码

    一.windows下修改MySQL用户密码的方法:   1.关闭正在运行的MySQL服务:net stop mysql  或 在windows 任务管理器中结束 mysqld.exe 进程 或 在 管 ...

  10. Android热修复AndFix

    热修复主要用来修复代码.修复bug.添加独立的功能,他的原理主要是操作PathClassLoader.DexClassLoader. PathClassLoader是类加载器,DexClassLoad ...