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.)
 class Solution {
// Approach 1
public int[][] kClosest1(int[][] points, int K) {
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((p1, p2) -> p2[] * p2[] + p2[] * p2[] - p1[] * p1[] - p1[] * p1[]);
for (int[] p : points) {
pq.offer(p);
if (pq.size() > K) {
pq.poll();
}
}
int[][] res = new int[K][];
while (K > ) {
res[--K] = pq.poll();
}
return res;
} // Approach 2
public int[][] kClosest2(int[][] points, int K) {
int len = points.length, l = , r = len - ;
while (l <= r) {
int mid = partition(points, l, r);
if (mid == K) {
break;
} else if (mid < K) {
l = mid + ;
} else {
r = mid - ;
}
}
return Arrays.copyOfRange(points, , K);
} private int compare(int[] p1, int[] p2) {
return p1[] * p1[] + p1[] * p1[] - p2[] * p2[] - p2[] * p2[];
} private int partition(int[][] A, int start, int end) {
int p = start;
for (int i = start; i <= end - ; i++) {
if (compare(A[i], A[end]) < ) {
swap(A, p, i);
p++;
}
}
swap(A, p, end);
return p;
} private void swap(int[][] nums, int i, int j) {
int[] temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

K Closest Points to Origin的更多相关文章

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

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

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

  4. LeetCode 973. K Closest Points to Origin

    原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...

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

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

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

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

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

随机推荐

  1. vue使用ajax

    1.Vue的Ajax基本用法 在vue中用Ajax需要用到vue.js和vue-resource.js; vue-resource.js的下载地址:https://cdn.staticfile.org ...

  2. vue模板语法上集

    模板语法上集 1.1 插值 1.1.1 文本 {{msg}} 1.1.2 html 使用v-html指令用于输出html代码 1.1.3 属性 HTML属性中的值应使用v-bind指令 1.1.4 表 ...

  3. 你知道 GNU Binutils 吗?【binutils】

    概述 从事 Linux 开发的朋友们都不可避免地用到一些工具,比如 objcopy.nm.objdump.readelf 等等.其实这一系列的工具,就是所谓的 Binutils,当然 GNU 就表示它 ...

  4. jQuery事件之一次性事件

    语法: $(selector).one(type, data, callback) 作用: (1)为每一个匹配元素的特定事件(比如click)绑定一个一次性的事件处理函数. (2)每个对象上,这个事件 ...

  5. [笔记]nginx配置反向代理和负载均衡

    1.nginx配置文件:源码安装情况下,nginx.conf在解压后的安装包内.yum安装,一般情况下,一部分在/etc/nginx/nginx.conf中,一部分在/etc/nginx/conf.d ...

  6. 微信公众号实现无限制推送模板消息!可向指定openID群发

    微信认证的服务号才有推送模板消息接口所以本文需要在认证服务号的情况下学习 以上就是模板消息,只有文字和跳转链接,没有封面图.在服务号的后台添加功能插件-模板消息即可. 模板消息,都是在后台选择一个群发 ...

  7. TCP之服务与首部

    1. TCP 的服务 TCP 通过下列方式提供可靠性: 应用数据被分割成 TCP 认为最适合发送的数据块.与 UDP 不同,UDP 应用程序产生的数据报长度将保持不变.由 TCP 传递给 IP 的信息 ...

  8. 备忘录:“#ifdef __cplusplus extern "C" { #endif”的定义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  9. Linux 解压小全

    .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .zip 解压:unzip FileName.zip 压缩:zi ...

  10. PHP JSON数据 AJAX

    JSON数据的定义方式 //写入数据 var a = { code:"p001", name:"张三", shuzu:new Array(1,2,3,4), j ...