原题链接在这里: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. HTTP下载图片

    网上有专门写的http下载的C++代码,但是我发现windows自带的http下载,一行代码就搞定,非常简单,目前为止使用正常. 首先包含头文件和lib文件 #include <urlmon.h ...

  2. Chunky Monkey

    猴子吃香蕉可是掰成好几段来吃哦! 把一个数组arr按照指定的数组大小size分割成若干个数组块. 例如:chunk([1,2,3,4],2)=[[1,2],[3,4]]; chunk([1,2,3,4 ...

  3. 连接GitHub的方法

    连接到GitHub 首先在本地创建 ssh key: ssh-keygen -t rsa -C "your_email@youremail.com" 后面的 your_email@ ...

  4. [C#]委托实例分析(附源码)

    一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...

  5. CF910C

    题解: 首先考虑暴力不行 然后采用贪心 按位展开 然后注意不能有前缀0 代码: #include<bits/stdc++.h> using namespace std; ],ans; ], ...

  6. 【javascript基础】JS计算字符串所占字节数

    废话不说,直接正题吧. 最近项目有个需求要用js计算一串字符串写入到localStorage里所占的内存,众所周知的,js是使用Unicode编码的.而Unicode的实现有N种,其中用的最多的就是U ...

  7. 程序包需要 NuGet 客户端版本“2.12”或更高版本,但当前的 NuGet 版本为“2.8.50313.46”

    由于安装install-package newtonsoft.json 会出现需要 NuGet 客户端版本“2.12”或更高版本来安装,于是换成旧版的newtonsoft.json   PM> ...

  8. 使用JQuery Deferred对象的then() 解决多个AJAX操作顺序依赖的问题

    原文地址:http://www.2cto.com/kf/201507/424202.html 之前的文章javascript异步代码的回调地狱中提到了编写AJAX代码经常遇到的3个问题,现在我们看下如 ...

  9. 使用nrm管理npm仓库

    使用nrm管理npm仓库 用npm装包的时候,经常碰到太慢或者npm官网被墙的情况,有时候凑合一下就改一下 "~/.npmrc" 文件,但是经常改来改去也挺麻烦的,于是找到了可以使 ...

  10. 如何去访问win8系统的共享文件夹

    关于共享,看过不少的贴子,觉得搞得很复杂.我看起来也很头痛.晕头转向的.其实我们作为家庭用户来说,不想搞得那么复杂,我们只要能做到能够从一台电脑访问另一台电脑的共享文件夹就可以了,这样我们就可以任意从 ...