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, ...
随机推荐
- springboot入门(一)--快速搭建一个springboot框架
原文出处 前言在开始之前先简单介绍一下springboot,springboot作为一个微框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...
- C#退出模式
1.this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit(); 强制所有消息中 ...
- English trip -- VC(情景课)4 D
What do you see? I can see three men in the pictrue. one older man is a doctor, two younger men are ...
- mysql--------char 和 varchar 的区别
char是一种固定长度的类型,varchar则是一种可变长度的类型,它们的区别是: char(M)类型的数据列里,每个值都占用M个字节,如果某个长度小于M,MySQL就会在它的右边用空格字符补足.(在 ...
- (转)nginx做转发时,带'_'的header内容丢失
原本在测试环境测试通过的APP,今天准备切到线上环境做最后测试,结果发现了错误.查看日志发现是APP端发送的http请求中的header内容丢失了.那么代码没有改动,怎么平白无故会丢失头信息? 于是想 ...
- mysql5.7执行sql语句出现only_full_group_by错误
在/etc/my.cnf的[mysqld]组中添加 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISI ...
- HDU-5050 Divided Land (二进制求GCD)
题目大意:将两个二进制数的GCD用二进制数表示出来. 题目分析:这道题可以用java中的大数类AC. 代码如下: import java.io*; import java.math.BigIntege ...
- inherit
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace { cl ...
- 无法打开物理文件“E:\Database\VRVIES6841-FZ01-Global\VRVEIS.mdf”。操作系统错误 5:“5(拒绝访问。)”
在用SQLServer2012附加SQLServer2000备份的数据库事,报如下错误: 无法打开物理文件“E:\Database\VRVIES6841-FZ01-Global\VRVEIS.mdf” ...
- MyBatis:4
转载:http://www.cnblogs.com/xrq730/p/5289638.html 什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相 ...