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 heap 做
/*
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
*/ public List<Point> findKClosest(Point[] p, int k) {
PriorityQueue<Point> pq = new PriorityQueue<>(10, new Comparator<Point>() {
@Override
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 (int i = 0; i < p.length; i++) {
if (i < k)
pq.offer(p[i]);
else {
Point temp = pq.peek();
if ((p[i].x * p[i].x + p[i].y * p[i].y) - (temp.x * temp.x + temp.y * temp.y) < 0) {
pq.poll();
pq.offer(p[i]);
}
}
} List<Point> x = new ArrayList<>();
while (!pq.isEmpty())
x.add(pq.poll()); return x;
}
Microsoft - Find the K closest points to the origin in a 2D plane的更多相关文章
- [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 ...
- 【leetcode】973. K Closest Points to Origin
题目如下: We have a list of points on the plane. Find the Kclosest points to the origin (0, 0). (Here, ...
随机推荐
- 笔试题-sql语句
今天遇到了不熟练(不会)的查询题目 回来自己又做了一下,如下 建表语句 -- Table structure for score -- ---------------------------- DRO ...
- Windows7安装JDK的环境变量设置javac不是内部命令或外部命令
转自:http://bbs.gfan.com/android-5941970-1-1.html Windows7安装JDK的环境变量设置 Windows7 X64安装“jdk-6u26-windows ...
- 雷林鹏分享:C# 索引器(Indexer)
C# 索引器(Indexer) 索引器(Indexer) 允许一个对象可以像数组一样被索引.当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样.您可以使用数组 ...
- 3-15 《元编程》第6章 3-16 hook method
Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Obje ...
- Form嵌入到Panel里(C#)
直接把这个 Form嵌入到一个 Panel中即可. 示例如下: 要嵌入的 Form: public partial class FormEmbed : Form { public FormEmbed( ...
- pycharm在创建py文件时,自动添加文件头注释
File -> settings -> Editor-> File and Code Templates -> Python Script 添加内容: #!/usr/bin/e ...
- jenkins+git+docker实验环境的搭建
持续集成(c/i)的实验环境 git/harbor服务器 ip 192.168.200.132 docker服务器 ip 192.168.200.149 Jenkins服务器 ...
- 51nod1210
题解: 二维树状数组,再矩阵推一下 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; int ...
- start with...connect by子句的浅用
start with的用法,其基本语法如下: select … from tablename start with 条件1connect by 条件2where 条件3; 但是我在pl/sql中写入以 ...
- 添加MyEclipse WebSphere Portal Server支持(二)
MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] 三.支持WebSphere Portal部署 当您为WebSphere Portal 7.0或8. ...