LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/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 distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000-10000 < points[i][0] < 10000-10000 < points[i][1] < 10000
题解:
用maxHeap来维护K shortest distence.
Time Complexity: O(nlogK). n = points.length.
Space: O(n).
AC Java:
class Solution {
public int[][] kClosest(int[][] points, int K) {
if(points == null || points.length == 0 || K < 1){
return points;
}
PriorityQueue<int []> pq = new PriorityQueue<int []>((a, b) -> getDistanceSquare(b)-getDistanceSquare(a));
for(int [] point : points){
pq.add(point);
if(pq.size() > K){
pq.poll();
}
}
int [][] res = new int[K][2];
for(int i = 0; i<K; i++){
res[i] = pq.poll();
}
return res;
}
private int getDistanceSquare(int [] point){
return point[0]*point[0] + point[1]*point[1];
}
}
LeetCode 973. K Closest Points to Origin的更多相关文章
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【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, ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- arm中的几个公式的比较
串口 UART0.UBRDIVO=0X4d; 设置波特率 12000000/9600/16 -1=77化为16进制就是4dADC AD converter freq =50MHZ/(49+1) =1M ...
- Java 练习题摘录
2018-01-21 23:23:08 1.finally与return同时出现的情况 finally中有return语句则会使try...catch中的return语句失效 public stati ...
- Linux常用插件
文件传输 虚拟机用xshell连接时,拖拽传输插件 yum install lrzsz setup界面化工具 yum install setuptool yum install ntsysv # ...
- confluence wiki 破解安装操作流程
准备postgres数据库安装 步骤1:命令: docker pull postgres 步骤2:安装: docker run --name postgresdb -p 5432:5432 -e PO ...
- IOS-每个程序员的编程之路上都应该看这11本书
国外知名网站stackoverflow上有一个问题调查: 哪本书是对程序员最有影响.每个程序员都该阅读的书?,这个调查已历时两年,目前为止吸引了153,432人访问,读者共推荐出了478本书(还在增加 ...
- Bootstrap 可视化布局--拖拽后弹窗进行编辑
Bootstrap 可视化布局--拖拽后弹窗进行编辑 最近后台想一个需求,使用可视化布局-中文 | en中拖拽表格后,弹窗进行编辑,保存下载后在后台生成pdf格式. 奈何各种问题不断,使用 jquer ...
- MySQL使用通用二进制格式安装
CentOS7安装MySQL的方法之通用二进制格式
- python获取文件路径, 文件名, 后缀名
def get_filePath_fileName_fileExt(fileUrl): """ 获取文件路径, 文件名, 后缀名 :param fileUrl: :ret ...
- JBPM4入门——1.jbpm简要介绍
本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...
- Valgrind的Memcheck快速入门
一.前言 对于C/C++程序员来说,关于内存问题真是让人头痛不已,尤其是内存泄露.使用未初始化的局部变量进行跳转或移动等隐形问题.要求程序员养成良好的编码习惯确实很重要,但是人总会出现稀 ...