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

  1. Input: points = [[1,3],[-2,2]], K = 1
  2. Output: [[-2,2]]
  3. Explanation:
  4. The distance between (1, 3) and the origin is sqrt(10).
  5. The distance between (-2, 2) and the origin is sqrt(8).
  6. Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
  7. We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].

Example 2:

  1. Input: points = [[3,3],[5,-1],[-2,4]], K = 2
  2. Output: [[3,3],[-2,4]]
  3. (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. }

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. python+opencv链接摄像头

    import cv2 import numpy as numpy cap=cv2.VideoCapture(0) #设置显示分辨率和FPS ,不设置的话会非常卡 cap.set(cv2.CAP_PRO ...

  2. 使用jquery.jqprint.js 实现的打印功能,IE9不能进行打印预览、火狐打印空白界面

    提示的内容:SCRIPT438: 对象不支持“ExecWB”属性或方法 首先解决IE9不能打印预览的问题: 查找了一大推资料 ,有两种说法:一种是IE的安全性级别太高:一种是需要安装什么   微软we ...

  3. [javascript]Dom操作笔记

    1.为一个节点同时设置多个属性 $("div[aria-describedby='F53_batch_history']").attr({"display":& ...

  4. spring mvc:输出json,输出多个json

    spring mvc:输出xml/输出json 用到的注解@ResponseBody @ResponseBody用来输出json/xml等格式数据(非html) controller输出用到的类 or ...

  5. Ghost:一款简约风格博客系统

    前言 本文将介绍一种最快速的创建Ghost博客系统的方法,并实现绑定二级域名到该博客系统.本文以本博客的“微博客”为例. 一键创建Ghost博客系统 Kite 是 Ghost 博客托管商,网址为:ht ...

  6. 获取url参数,替换特殊字符

    function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...

  7. DXVA2解码数据用texture纹理渲染

    FFmpeg DXVA2解码得到的数据使用surface来承载的,surface限制很多,如果能用纹理来渲染的话,那我们就可以充分开发D3D,比如可以用坐标变换来实现电子放大的功能,还可以用坐标变换来 ...

  8. 卸载Linux自带的JDK

    Redhat Enterprise Linux中自带了jdk的旧版本,往往需要卸载,卸载步骤如下: 在终端输入:yum remove java 终端显示:Is this ok[y/N]: 输入y,按回 ...

  9. Ansible 小手册系列 四(详解配置文件)

    [root@host-172-20-6-120 ansible]# ansible --version ansible 2.2.0.0 config file = /etc/ansible/ansib ...

  10. oracle实例内存(SGA和PGA)调整-xin

    一.名词解释 (1)SGA:System Global Area是Oracle Instance的基本组成部分,在实例启动时分配;系统全局域SGA主要由三部分构成:共享池.数据缓冲区.日志缓冲区. ( ...