K Closest Points(sort)
Given somepoints
and anorigin
point in two-dimensional space, findk
points which are nearest to theorigin
.
Return these points sorted by distance, if they are same in distance, sorted by the x-axis, and if they are same in the x-axis, sorted by y-axis.
Example
Given points =[[4,6],[4,7],[4,4],[2,5],[1,1]]
, origin =[0, 0]
, k =3
return[[1,1],[2,5],[4,4]]
分析
按着dist,x,y加入一个List,然后sort
取出来前K个,记得这里是Obj,所以要组装一下。
2点距离公式 sqrt((x1-x2)^2) + sqrt((y1-y2)^2)
top k smallest
maxheap -cmp size k
minheap pop k times
Last updated