原题链接在这里: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:

 1 class Solution {
 2     public int[][] kClosest(int[][] points, int K) {
 3         if(points == null || points.length == 0 || K < 1){
 4             return points;
 5         }
 6
 7         PriorityQueue<int []> pq = new PriorityQueue<int []>((a, b) -> getDistanceSquare(b)-getDistanceSquare(a));
 8         for(int [] point : points){
 9             pq.add(point);
10             if(pq.size() > K){
11                 pq.poll();
12             }
13         }
14
15         int [][] res = new int[K][2];
16         for(int i = 0; i<K; i++){
17             res[i] = pq.poll();
18         }
19
20         return res;
21     }
22
23     private int getDistanceSquare(int [] point){
24         return point[0]*point[0] + point[1]*point[1];
25     }
26 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/10556223.html

LeetCode 973. K Closest Points to Origin相关推荐

  1. Leetcode PHP题解--D29 973. K Closest Points to Origin

    973. K Closest Points to Origin 题目链接 973. K Closest Points to Origin 题目分析 给一个坐标数组points,从中返回K个离0,0最近 ...

  2. LeetCode 973. K Closest Points to Origin--TopK 问题--最小堆--C++,Python解法

    题目地址:K Closest Points to Origin - LeetCode We have a list of points on the plane. Find the K closest ...

  3. LeetCode Daily challenge - K Closest Points to Origin

    文章目录 题目大意 思路 代码 总结 题目大意 找前K小元素,只不过元素是二维的坐标 思路 很容易想到排序后取前K个,虽然可以但是速度不够快.实际上类似于找第K大(小),可以用划分的方法找,每次随机划 ...

  4. LeetCode 973. 最接近原点的 K 个点(排序/优先队列/快排)

    文章目录 1. 题目 2. 解题 2.1 排序 2.2 优先队列 2.3 快排思路 1. 题目 我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. ...

  5. LeetCode:658. Find K Closest Elements程序分析

    好久没有练习算法了,以此纪研究生以来第一次练习算法题. 原题链接:https://leetcode.com/problems/find-k-closest-elements/description/ ...

  6. [Leetcode]658. Find K Closest Elements

    # [[Leetcode]658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) - ...

  7. 迭代最近点算法 Iterative Closest Points

    研究生课程系列文章参见索引<在信科的那些课> 基本原理 假定已给两个数据集P.Q, ,给出两个点集的空间变换f使他们能进行空间匹配.这里的问题是,f为一未知函数,而且两点集中的点数不一定相 ...

  8. LeetCode 787. K 站中转内最便宜的航班(图/Bellman Ford算法)

    文章目录 贝尔曼-福特算法(Bellman-Ford) 简介 算法思想 算法执行过程 应用 题目描述 分析 代码 LeetCode 787. K 站中转内最便宜的航班 题目描述 Bellman For ...

  9. LeetCode——787. K 站中转内最便宜的航班(Cheapest Flights Within K Stops)[中等]——分析及代码(Java)

    LeetCode--787. K 站中转内最便宜的航班[Cheapest Flights Within K Stops][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...

  10. LeetCode 787.K站中转内最便宜的航班

    LeetCode 787.K站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 ds ...

最新文章

  1. js正则验证身份证号是否正确
  2. Ubuntu双系统Grub启动菜单修复
  3. JetBrains遭美国调查,称其是被大规模黑客攻击的源头?
  4. 戴口罩也能刷门禁?疫情下AnalyticDB亮出社区管理的宝藏神器!
  5. JavaScript编程语言 基础 (1)
  6. publiccms按照指定显示的日期格式,格式化日期的写法
  7. java学习(77):GUL下拉菜单框和滚动条
  8. 【美文保存】nosql数据库对比以及如何巧妙利用redis来提高效率?
  9. 遍历字典_十三、Python字典三种遍历方法
  10. 医用口罩、N95、KN95口罩的区别
  11. shellcode之简单的栈溢出实验
  12. 组装台式计算机配置清单,diy之家 - 2017组装电脑配置清单_电脑diy主机配置推荐...
  13. cubieboard2 android,在cubieboard2双卡版上从零构建Android4.2.2系统
  14. 【215期推荐】另类思考:HIS能给医院带来什么“坏处”?
  15. HDLBITS笔记15:组合逻辑之7420芯片
  16. 极速办公如何在Excel中进行条件计数
  17. TypeError: Descriptors cannot not be created directly解决
  18. 基于QQ远程协助的远程桌面实现
  19. 服务器虚拟机租用价格,云服务器虚拟机租用
  20. 帝国cms推送插件-帝国cms管理系统-帝国cms免费模板插件

热门文章

  1. The content of the adapter has changed but ListView did not receive a notification
  2. 在类库中使用Session
  3. [整理]解析Json需要设置Mime
  4. Asp.Net 之Jquery知识点运用
  5. IOI flower
  6. 2008中国最佳寓言
  7. win10+anaconda3+python3.7+pytorch-cpu安装
  8. WWW2022 | 知识提示的预训练微调
  9. 【CIKM2020】如何更为合适地评测推荐算法? Top-N物品推荐算法评测设置回顾
  10. 基于BERT的ASR纠错