K closest points
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的更多相关文章
- [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, ...
- 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 ...
- 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 ...
随机推荐
- ZooKeeper 笔记(1) 安装部署及hello world
先给一堆学习文档,方便以后查看 官网文档地址大全: OverView(概述) http://zookeeper.apache.org/doc/r3.4.6/zookeeperOver.html Get ...
- 2016-2017-2 《Java程序设计》预备作业2总结
2016-2017-2 <Java程序设计>预备作业2总结 古希腊学者普罗塔戈说过:「头脑不是一个要被填满的容器,而是一束需要被点燃的火把.」 在对计算机系的学生情况的调查中,我说: 最近 ...
- IIS安装
引自:http://www.cnblogs.com/Joans/archive/2012/07/16/2593828.html
- Poisson泊松分布
PMF 若随机变量\(K\)的概率质量函数PMF为 \[ P(K = k) = e^ {-\lambda} \frac {\lambda^k}{k!} \] 则称:\(K \sim Poisson(\ ...
- bzoj1208
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7589 Solved: 3009[Submit][Sta ...
- WebView·开发指南
WebView·开车指南 作者:凌俊琦链接:https://zhuanlan.zhihu.com/p/22247021来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. T ...
- Matplotlib 学习笔记
注:该文是上了开智学堂数据科学基础班的课后做的笔记,主讲人是肖凯老师. 数据绘图 数据可视化的原则 为什么要做数据可视化? 为什么要做数据可视化?因为可视化后获取信息的效率高.为什么可视化后获取信息的 ...
- “is null”与“=”的使用
普通的值可以进行"="操作,例如条件中一般都会这样出现:sUserName='张三',如果sUserName的值为null,要想找出所 有名字为null的记录时,不能这样用:sUs ...
- zookeeper系列之通信模型(转)
本文的主题就是讲解Zookeeper通信模型,本节将通过一个概要图来说明Zookeeper的通信模型. Zookeeper的通信架构 在Zookeeper整个系统中,有3中角色的服务,client.F ...
- 【Beta版本】冲刺随笔汇总
[Beta版本]冲刺计划及安排 [Beta版本]冲刺-Day1 [Beta版本]冲刺-Day2 [Beta版本]冲刺-Day3 [Beta版本]冲刺-Day4 [Beta版本]冲刺-Day5 [Bet ...