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 ...
随机推荐
- javaScript tips —— z-index 对事件机制的影响
demo // DOM结构 class App extends React.Component { componentDidMount() { const div1 = document.getEle ...
- PHPCMSV9的CKEDITOR编辑器增加行距
lineheight插件,下载地址:http://files.cnblogs.com/ysfng/ckeditor-lineheight.zip 第一步,下载lineheight插件,并解压到\cke ...
- HttpConnection的使用
项目中需要与第三方系统交互,而交互的方式是XML报文形式,所以会用到HttpConnection与第三方系统连接交互,使用起来并不复杂,但是有几点需要注意的: 1.乱码的问题解决 2.超时的设置,注意 ...
- innerHTML的兼容性
问题描述: 给定一个表格,thead的内容一致,tbody的内容动态改变(内容,合并单元格等不同) 错误方案: 给tbody定义一个id,然后document.getElementById('id') ...
- 使用IScroll5实现滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Hibernate入门1. Hibernate基础知识入门
Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...
- C#学习历程(七)[基础知识]
---恢复内容开始--- >>接受用户输入的整数 Console.readline():接受键盘输入的字符串,如果需要接受整数并输出,则需要字符串的转换. 一般建议使用Covert类中的方 ...
- Git添加远程库
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Redis数据结构:跳跃表
1. 跳跃表是有序集合(zset)的底层实现之一: 2. 由zskiplist和zskiplistNode组成: 3. 每个跳跃表节点的层数都是1-32之间的随机数(每创建一个节点的时候,程序会随机生 ...
- 第5课:内置函数、处理json、常用模块
1. 内置函数 1)常用的内置函数 print(all([0, 2, 3, 4])) # False,判断可迭代的对象里面的值是否都为真 print(any([0, 1, 2, 3, 4])) # T ...