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 ...
随机推荐
- 简单的尝试下angr
0x00:前言 之前接触到了符号执行,可以用于程序的自动化分析,感觉还是比较神奇,工业上的具体用法不是很清楚,不过在CTF中这个东西慢慢在流行...从defcon 2016就可以看出(有很多人解re用 ...
- 2019牛客暑期多校训练营(第二场)J
题意 给一个长度为1e9的只包含1和-1的数列,1的个数不超过1e7,计算有多少对\((l,r)\)满足\(\sum_{i=l}^r a[i]>0\) 分析 dp求出每段连续的1最右端为右端点的 ...
- svn的下载与安装,使用,包教包会!!!
svn的安装使用说明 下载svn服务器与搭建 高效开发 — SVN使用教程(客户端与服务端安装详解!带图!带注释!安装客户端与服务端的地址可以看上两个链接) svn安装分为两部分,服务端安装与客户端安 ...
- JSP通过URL给Servlet传值
jsp传数据: <a id="a1" href="" ></a> <script> $("#a1").a ...
- css基础(浮动 清除f浮动)
文档流(标准流) 1.元素自上而下,自左而右 2.块元素,独占一行,行内元素在一行上显示,碰到父级元素的边框换行 浮动left 浮动的框可以向左或是向右移动,直到它的边缘碰到包含框或是另个浮动框 ...
- [BZOJ1123]:[POI2008]BLO(塔尖)
题目传送门 题目描述 Byteotia城市有n个towns.m条双向roads.每条road连接两个不同的towns,没有重复的road.所有towns连通. 输入格式 输入n,m及m条边. 输出格式 ...
- [题解] [TJOI2011] 构造矩阵
题面 题解 很容易看出来是道网络流的题目, 要是没有这个字典序最小, 直接建图跑一遍就好了, 考虑如何输出字典序最小的方案 我们可以贪心地去选择, 若当前点可以选0就选0, 不能选0就选1, 有一点像 ...
- linux 分区管理
1. 查看系统中硬盘的设备 [root@centos6 ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb 可以看出,系统有 ...
- 朴素贝叶斯文本分类-在《红楼梦》作者鉴别的应用上(python实现)
朴素贝叶斯算法简单.高效.接下来我们来介绍其如何应用在<红楼梦>作者的鉴别上. 第一步,当然是先得有文本数据,我在网上随便下载了一个txt(当时急着交初稿...).分类肯定是要一个回合一个 ...
- Mybaits和Spring的那点事
前言 在spring中使用mybaits简直不要太简单,只需要几个配置,一个DAO接口和一个mapper.xml就可以完成一次数据库交互.但是简单背后往往是复杂的实现,现在我们来探讨一下里面的一点原理 ...