• Difficulty: Easy

Problem

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

Note:

  1. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -10000 < points[i][1] < 10000

Related Topics

Math, Divide and Conquer, Sort

Solution

解法显而易见,按照到原点的距离升序排列,取出前 K 个即可。由于 \(f(x) = \sqrt{x}\) 在其定义域上单调递增,故只需要计算根号内的 \(x\),即 \(x^2 + y^2\) 即可。

public class Solution
{
public int[][] KClosest(int[][] points, int K)
{
Array.Sort(points, delegate (int[] x, int[] y)
{
return (x[0] * x[0] + x[1] * x[1]) - (y[0] * y[0] + y[1] * y[1]);
});
int[][] ret = new int[K][];
Array.Copy(points, ret, K);
return ret;
}
}

在查看其他人的解答时,发现了如下解答(这个解答的作者的 LINQ 玩得很熟)

// no need to sqrt sinc we only want
public class Solution
{
public int[][] KClosest(int[][] points, int K)
{
return points
.OrderBy(x => x[0] * x[0] + x[1] * x[1])
.Take(K)
.ToArray();
}
}

[Solution] 973. K Closest Points to Origin的更多相关文章

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

  2. LeetCode 973. K Closest Points to Origin

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

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

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

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

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

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

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

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

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

随机推荐

  1. .net平台常用组建

    常用的一些开源组件整理: 导出Excel报表的插件:NOPI.dll(基于微软OpenXml实现)开源的作业调度和自动任务框架:Quartz.NET用于大数据搜索引擎的全文检索框架:Lucene.ne ...

  2. Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)

    题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...

  3. PythonStudy——函数的参数 Function argument

    形参与实参 # 参数介绍:# 函数为什么要有参数:因为内部的函数体需要外部的数据# 怎么定义函数的参数:在定义函数阶段,函数名后面()中来定义函数的参数# 怎么使用函数的参数:在函数体中用定义的参数名 ...

  4. /etc/hosts和/etc/hostname区别

    /etc/hosts主要是ip和域名的对应 /etc/hostname主要是本地主机域名(本地主机名修改过后需要重启服务器才能生效) 如果我想在另一台linux主机里面使用域名访问上面这台主机A,只需 ...

  5. Netty学习记录

    一.Netty简介 Netty 是一个基于 JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. Netty 是一个 NIO client-s ...

  6. NTP搭建

    NTP(Network Time Protocol,网络时间协议),用于同步它所有客户端时钟的服务.NTP服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使本地网络的所有客 ...

  7. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  8. firefox修改语言

    下面咱们就可以开始更改设置来让咱们安装好的语言成为默认的语言. 首先在空窗口里输入以下地址:about:config,进入设置页面. 2 请大家定位到general.useragent.locale这 ...

  9. Struts2 中常用的代码

    BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...

  10. Using a ScrollView - RN4

    使用滚动条. 1. import import {ScrollView} from "react-native"; 2. Using <ScrollView> ... ...