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.)
 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的更多相关文章
- [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 ... 
- [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 ... 
- 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, ... 
- LeetCode 973. K Closest Points to Origin
		原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 【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, ... 
- 【LeetCode】973. K Closest Points to Origin 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ... 
随机推荐
- CDOJ   1255    斓少摘苹果  图论     2016_5_14
			斓少摘苹果 Time Limit: 3000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ... 
- objdump命令解析
			[objdump] 相关链接: 实例分析objdump反汇编用法 - 在路上 - CSDN博客 https://blog.csdn.net/u012247418/article/details/80 ... 
- 推荐系统系列(一):FM理论与实践
			背景 在推荐领域CTR(click-through rate)预估任务中,最常用到的baseline模型就是LR(Logistic Regression).对数据进行特征工程,构造出大量单特征,编码之 ... 
- MySQL_(Java)提取工具类JDBCUtils
			MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC创建用户名和密码校验查询方法 传送门 MySQL_(Java)使用preparestatement ... 
- mysql基础知识语法汇总整理(二)
			mysql基础知识语法汇总整理(一) insert /*insert*/ insert into 表名(字段列表) values(值列表); --蠕虫复制 (优点:快速复制数据,测试服务器压力) in ... 
- MVC中上传文件
			与asp.net中几乎一样,使用表单提交的方式上传文件(如果是使用了第三方插件的话,那么就另当别论) @{ ViewBag.Title = "Index"; Layout = nu ... 
- 尚学堂requireJs课程---3、私有和公有属性和方法
			尚学堂requireJs课程---3.私有和公有属性和方法 一.总结 一句话总结: 在 [模块] 的基础上,在return对象里面的方法和属性就是公有的(因为外部可以访问),不在的就是私有的 < ... 
- redux异步
			在一个项目中 redux 是必不可少的,redux 中没有提供异步的操作,但是异步又是项目开发中重要的一部分,所以我们的 redux 对此有进行了拓展: 所以我们需要 redux-thunk 的插件, ... 
- php 获取域名
			echo 'SERVER_NAME:'.$_SERVER['SERVER_NAME']; //获取当前域名(不含端口号) echo '<p>'; echo 'HTTP_HOST:'. ... 
- [go]go环境安装-解决安装包不能访问golang.org问题
			安装go和vscode vscode插件列表选择go,安装即可,其他插件暂不安装 手动安装一些vscode配套的调试工具等 直接vscode-go,然后点下面的go-tools就能找到 go get ... 
