LeetCode 973. K Closest Points to Origin--TopK 问题--最小堆--C++,Python解法
题目地址:K Closest Points to Origin - LeetCode
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 <= K <= points.length <= 10000
- -10000 < points[i][0] < 10000
- -10000 < points[i][1] < 10000
这道题目是找前TopK小的元素,最容易想到的方法是全排序,然后取前K个。
Python解法如下:
class Solution:def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:res = []if len(points) == 0:return resfor i in points:i.append(i[0]**2+i[1]**2)points.sort(key=lambda x: x[2])for i in range(0, K):res.append([points[i][0],points[i][1]])return res
简化一下:
class Solution:def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:return sorted(points,key = lambda x:x[0]*x[0]+x[1]*x[1])[:K]
使用最小堆可以稍微快一些:
import heapq
class Solution:def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:return heapq.nsmallest(K,points,key=lambda x: x[0]*x[0]+x[1]*x[1])
C++解法如下:
class Solution
{public:vector<vector<int>> kClosest(vector<vector<int>> &points, int K){sort(points.begin(), points.end(), [](vector<int> &a, vector<int> &b) {return a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1];});return vector<vector<int>>(points.begin(), points.begin() + K);}
};
LeetCode 973. K Closest Points to Origin--TopK 问题--最小堆--C++,Python解法相关推荐
- Leetcode PHP题解--D29 973. K Closest Points to Origin
973. K Closest Points to Origin 题目链接 973. K Closest Points to Origin 题目分析 给一个坐标数组points,从中返回K个离0,0最近 ...
- LeetCode Daily challenge - K Closest Points to Origin
文章目录 题目大意 思路 代码 总结 题目大意 找前K小元素,只不过元素是二维的坐标 思路 很容易想到排序后取前K个,虽然可以但是速度不够快.实际上类似于找第K大(小),可以用划分的方法找,每次随机划 ...
- merge k sorted lists java_LeetCode 第23题 Merge k Sorted Lists【分而治之】【最小堆】(Java)...
这道题的题目是合并k个有序的链表,标定难度为Hard,详细需求: 合并k个有序的链表,返回一个新的有序链表. 例子: Input: 1->4->5, 1->3->4, 2-&g ...
- LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法
题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...
- LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法
题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- LeetCode题解(1647):字符频次唯一的最小删除次数(Python)
题目:原题链接(中等) 标签:贪心算法.排序 解法 时间复杂度 空间复杂度 执行用时 Ans 1 (Python) O(NlogN)O(NlogN)O(NlogN) O(N)O(N)O(N) 132m ...
- LeetCode刷题之python解法(持续更新)
1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...
- LeetCode 973. 最接近原点的 K 个点(排序/优先队列/快排)
文章目录 1. 题目 2. 解题 2.1 排序 2.2 优先队列 2.3 快排思路 1. 题目 我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. ...
最新文章
- Vue注意事项及用得较多的属性归纳
- 什么是区块链钱包?区块链钱包如何运作?
- spring源码分析第六天------spring经典面试问题
- 向日葵远程控制软件——使用方法(含MacOS)
- 人体时钟屏保-widows电脑屏幕保护
- 移远M26 GSM实时获取网络时间
- Kubuntu终端中文显示一半解决办法
- 数据结构教程(详细又简单——C语言实现)
- 2015年8月4日工作日志--------赵鑫
- 实现isPrime()函数,参数是整数,如果整数是质数, 返回True,否则返回False
- web防火墙和waf防火墙的区别和选择
- Linux的ssh学习与配置(SSH的登录)
- 记录贴/阴阳师core loop
- 一、在PyCharm上直接调试py脚本
- C#中调用Flash按钮,点击按钮可打开相应的功能
- Java统计中的计数方法
- codeforces#1166F. Vicky's Delivery (Service并查集+启发式合并)
- PyCharm里面的c、m、F、f、v、p分别代表什么含义?
- 如今的互联网还是不是最值得加入的行业 #从熊猫败局聊起(转载)
- 调整idea面板字体大小
热门文章
- Logistic Regression
- Excel VBA参考文献中人名与年份格式转换
- 临床基因组学数据分析实战助力解析Case,快速发表文章
- iMeta期刊投审稿系统ScholarOne正式上线
- Science:比较基因组揭示银边鱼应对捕鱼行为的表型进化机制
- pandas使用itertuples函数迭代dataframe中的数据行并自定义修改行中的数值(update row while iterating over the rows)
- python使用numpy中的np.mean函数计算数组的均值、np.var函数计算数据的方差、np.std函数计算数组的标准差
- R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(分组调色板填充、自定义调色板、灰度比例)实战(dot plot)
- R语言ggplot2可视化柱状图并自定义柱体的宽度(通过变量指定条形的宽度)实战、条形图并自定义条形的宽度实战
- R语言ggplot2可视化使用ggsave将可视化图像结果保存为SVG文件实战