原题链接在这里: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. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -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的更多相关文章

  1. 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, ...

  2. 【LeetCode】973. K Closest Points to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  3. 【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, ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. flask学习(九):模板渲染和参数传递

    一. 如何渲染模板 1. 模板放在templates文件夹下 2. 从flask中导入render_template函数 3. 在视图函数中,使用render_template函数,渲染模板 注意:只 ...

  2. uva10766生成树计数

    此类题是给定一个无向图,求所有生成树的个数,生成树计数要用到Matrix-Tree定理(Kirchhoff矩阵-树定理) G的度数矩阵D[G]是一个n*n的矩阵,并且满足:当i≠j时,dij=0:当i ...

  3. 5.彻底理解volatile

    1. volatile简介 在上一篇文章中我们深入理解了java关键字synchronized,我们知道在java中还有一大神器就是关键volatile,可以说是和synchronized各领风骚,其 ...

  4. 二十三 Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制

    用命令创建自动爬虫文件 创建爬虫文件是根据scrapy的母版来创建爬虫文件的 scrapy genspider -l  查看scrapy创建爬虫文件可用的母版 Available templates: ...

  5. js中的真值和假值

    大多数编程语言中,布尔值true和false仅仅表示true/false.JavaScript中,如'Hello‘这样的字符串值,也可以看做true. 以下是不同数据类型在JavaScript中是如何 ...

  6. Markdown - 让网络书写变得简单

    概述 宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所 ...

  7. RMAN中format的参数

    (转自:http://blog.chinaunix.net/uid-23079711-id-2554290.html) format 的替换变量,注意大小写! 1.     %d --数据库的db_n ...

  8. dell c6220II lsi阵列卡

    1.如果在lsi阵列卡上有多个raid,那么需要在第一个创建的raid上装系统,或者说先创建装系统的raid,否则可能报 hard disk error(centos 6.6) 2.热插拔的后果:如果 ...

  9. querySelectorAll 与jquery.find 与htmlcollection 的区别

    querySelector 和 querySelectorAll 规范定义 querySelector 和 querySelectorAll 方法是 W3C Selectors API Level 1 ...

  10. hdu4685

    题解: 二分图匹配 对于每一个单身狗 见一个虚拟的人 然后就可以做了 代码: #include<cstdio> #include<cstring> #include<al ...