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, ... 
随机推荐
- Java 常用对象-基本类型的封装类
			2017-11-04 20:39:26 基本类型封装类:基本类型的封装类的好处是可以在对象中定义更多的功能方法操作该数据. 常用操作之一:用于基本数据类型与字符串的转换. 基本类型和包装类的对应: b ... 
- ArcGIS Runtime数据制作教程
			分类: ArcGIS Runtime 2012-04-20 22:25 879人阅读 评论(0) 收藏 举报 工具测试文档file工作c ArcGIS Runtime不能直接加载shp数据,或者mxd ... 
- 4-1 contag_tag:返回HTMLtag.
			jquery已经过时,做一遍,了解其他知识点. contag_tag(name, content_or_options_with_block = nil, options = nil, &bl ... 
- codeforces 578b//"Or" Game// Codeforces Round #320 (Div. 1)
			题意:n个数字,最多操作k次,每次乘x,要使结果数组的与值最大. 能推断出结果是对一个元素操作k次,并且这个元素的二进制最高位比较大.并不一定是取最大的,比如1100和1010,乘以一次2,两种选法分 ... 
- python-day20--正则表达式与re模块
			1.通过re模块可以做一些关于正则的相关操作 2.正则表达式:做字符串匹配的规则 1)字符组:在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[ ]表示 [0-9][a-f][A-F] ... 
- 自定义Spark Partitioner提升es-hadoop Bulk效率
			http://www.jianshu.com/p/cccc56e39429/comments/2022782 和 https://github.com/elastic/elasticsearch-ha ... 
- java并发编程:线程安全管理类--原子操作类--AtomicLongArray
			1.类 AtomicLongArray public class AtomicLongArray extends Object implements Serializable 可以用原子方式更新其元素 ... 
- 快速切题 acdream手速赛(6)A-C
			Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ... 
- 《Python》 函数进阶和名称空间作用域
			函数进阶: 一.动态参数:*args **kwargs *args是元祖形式,接收除去键值对以外的所有参数 # args可以换成任意变量名,约定俗成用args **kwargs接收的只是键值对的参数 ... 
- CUDA ---- Dynamic Parallelism
			Dynamic Parallelism 到目前为止,所有kernel都是在host端调用,GPU的工作完全在CPU的控制下.CUDA Dynamic Parallelism允许GPU kernel在d ... 
