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. Jmeter工具使用-分布式架构和服务器性能监控解决方案

    在对项目做大并发性能测试时,常会碰到并发数比较大(比如需要支持10000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter提供的分布式测试的功能来搭建分布式并发环境. 一.J ...

  2. 【BZOJ4671】 异或图

    Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中, 否 ...

  3. jsPDF – 基于 HTML5 的强大 PDF 生成工具

    jsPDF 是一个基于 HTML5 的客户端解决方案,用于生成各种用途的 PDF 文档. 使用方法很简单,只要引入 jsPDF 库,然后调用内置的方法就可以了. 米扑科技项目用到了HHTML5生成PD ...

  4. hadoop hdfs hbase优化实例

    需求描述: 从hdfs中获取数据,字段url需要计算出url_type 通过进行hive的left outer join ,效率非常低.故将url的类型导入到hbase中,利用hbase快速查询的特点 ...

  5. Linux下MySQL报Table 'xxx' doesn't exist错误解决方法,表名存在大小写区分

    Linux服务器上在线装了个MySQL,但是部署web应用时一直报后台一直报错:Table 'xxx' doesn't exist. 本地测试一直都是正常的,同样的代码,同样的数据库,表是存在的,但是 ...

  6. (十八)C语言之预编译命令、宏

  7. Laravel find in set排序

    做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列.例如,用户状态有四种: 0=>未激活:1=>正常:2=>禁用:3=>软删除 现在的需求是,我要按照:正常-> ...

  8. Java-UncaughtExceptionHandler 捕获线程异常

    实现 UncaughtExceptionHandler 类,重写 uncaughtException 方法. public class MyUncaughtExceptionHandler imple ...

  9. http1.1管线话 vs htttp2.0 多路复用

    图中第一种请求方式,就是单次发送request请求,收到response后再进行下一次请求,显示是很低效的. 于是http1.1提出了管线化(pipelining)技术,就是如图中第二中请求方式,一次 ...

  10. HTML页面间传值

    页面一: window.location="./showUserMsg.html?IDno="+IDno+"&&thedate="+thedat ...