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. 导出excel 各 cvs 的方法

    public function orderExcelExport($data,$filename='simple.xls'){ ini_set('max_execution_time', '0'); ...

  2. ASCII和UTF-8

    造冰箱的熊猫@cnblogs 2018/12/11 用了这么久的UTF-8,第一次了解了点UTF-8的细节 UTF-8[1]属于变长度编码.一个UTF-8字符的编码长度为1~4个字节. 1)长度为1个 ...

  3. 内存拷贝函数 memcpy

    windows下实现: void* __cdecl memcpy(void* dst,const void* src,size_t count) { void*ret=dst; #if defined ...

  4. 生成CA免费证书

    环境 OpenSSL 1.0.2k FireFox 60.0 64位 Chrome 66.0.3359.181 (正式版本)(32位) Internet Explorer 11.2248.14393. ...

  5. 关于vue-resource 跨域请求的异常处理方法

    当你启动一个vue项目时,项目会运行在一个webpack的服务上,所以此时去访问其他端口或者是其他地址时,属于跨域请求,故会报异常. has been blocked by CORS policy: ...

  6. php 解析json失败,解析为空,json在线解析器可以解析,但是json_decode()解析失败(原)

    $str2='{"code":200,"datas":{"id":1,"coupon_id":"123&quo ...

  7. [译]Webpack 4 — 神秘的SplitChunksc插件

    原文链接:Webpack 4 - Mysterious SplitChunks Plugin 官方发布了 webpack 4,舍弃了之前的 commonChunkPlugin,增加了 SplitChu ...

  8. 4 Java 选择排序

    1 基本思想 在未排序序列中找到最小元素,存放到未排序序列的起始位置.在所有的完全依靠交换去移动元素的排序方法中,选择排序属于非常好的一种算法,需要对比len-n-1次,但是只交换1次或者0次. 2 ...

  9. Git 推送文件到远程仓库

    Configure Git for the first time: git config --global user.name "xxxxx xx"git config --glo ...

  10. ubuntu下tomcat运行不起来解决

    报错Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these enviro ...