答案 0 :(得分:34)
解决方案1 制作大小为K的堆,并按最小距离 O(NLogK)复杂度收集点数。
解决方案2 : 取大小为N的数组,按距离排序。应该使用QuickSort(Hoare修改)。 作为答案采取前K点。 这也是NlogN的复杂性,但可以优化以近似O(N)。 如果跳过不必要的子数组的排序。当您将数组拆分为2个子数组时,您应该只接受Kth索引所在的数组。 复杂度为:N + N / 2 + N / 4 + ... = O(N)。
解决方案3 :在结果数组中搜索Kth元素,然后将所有点设置为较小。存在 O(N) alghoritm,类似于搜索中位数。
注释:最好使用sqr of distance来避免sqrt操作,如果point有整数坐标,它会更快。
作为面试答案,最好使用解决方案2或3。
答案 1 :(得分:8)
答案 2 :(得分:3)
您可以使用KD树http://en.wikipedia.org/wiki/K-d_tree来划分空间,并且您可以使用二进制搜索逐渐搜索邻居。 使用这种方法的好处是,当您在运行时逐个或批量接收点/查询时,它可以轻松扩展到在线版本。
答案 3 :(得分:1)
解决方案1
private List<Point> nearestKPoint_1(List<Point> list, final Point center, int k) {
List<Point> ans = new ArrayList<>();
PriorityQueue<Point> maxHeap = new PriorityQueue<>(k + 1, new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
return distance(center, o2) - distance(center, o1);
}
});
for (Point p : list) {
maxHeap.offer(p);
if (maxHeap.size() > k) {
maxHeap.poll();
}
}
Iterator<Point> i = maxHeap.iterator();
while (i.hasNext()) {
ans.add(i.next());
}
return ans;
}
public int distance(Point p1, Point p2) {
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
static class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
return y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
解决方案2
private List<Point> nearestKPoint_2(List<Point> list, final Point center, int k) {
List<Point> ans = new ArrayList<>();
Distance[] nums = new Distance[list.size()];
for (int i = 0; i < nums.length; i++) {
nums[i] = new Distance(distance(center, list.get(i)), i);
}
quickSelect(nums, k);
for (int i = 0; i < k; i++) {
ans.add(list.get(nums[i].i));
}
return ans;
}
private void quickSelect(Distance[] nums, int k) {
int start = 0, end = nums.length - 1;
while (start < end) {
int p = partition(nums, start, end);
if (p == k) {
return;
} else if (p < k) {
start = p + 1;
} else {
end = p - 1;
}
}
}
private int partition(Distance[] nums, int start, int end) {
Distance pivot = nums[start];
int i = start, j = end + 1;
while (true) {
while (i < end && nums[++i].compareTo(pivot) < 0);
while (j > start && nums[--j].compareTo(pivot) > 0);
if (i >= j) {
break;
}
swap(nums, i, j);
}
swap(nums, start, j);
return j;
}
private void swap(Distance[] nums, int i, int j) {
Distance tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
class Distance implements Comparable<Distance> {
int d;
int i;
public Distance(int d, int i) {
this.d = d;
this.i = i;
}
@Override
public int compareTo(Distance o) {
return this.d - o.d;
}
}
答案 4 :(得分:0)
// point_type pt, length_sq(p) { return pt[0] * pt[0] + pt[1] * pt[1]}
// std::vector<point_type> points to search.
// The algorithm should recursion depth to
// O(k * log(points.size())), and
// running time to O(points.size()).
std::nth_element(
points.begin(),
points.begin() + k,
points.end(),
[&pt](point_type const & a)
{
return length_squared(a - pt);
});
// points[0], ... , points[k - 1] are the closest points to pt
答案 5 :(得分:0)
class Solution {
public int[][] kClosest(int[][] points, int K) {
double [] combinationArr = new double[points.length];
Hashtable<Double,int[]> pt = new Hashtable();
for (int i = 0; i <points.length; i++) {
int [] in = points[i];
for (int j = 0; j < in.length - 1; j++) {
Integer x = in[j];
Integer y = in[j + 1];
double powerX=Math.pow(x, 2);
double powerY = Math.pow(y, 2);
double combination= (Double)(Math.sqrt(powerX + powerY));
pt.put(combination, points[i]);
combinationArr[i] = combination;
}
}
Arrays.sort(combinationArr);
int [][] kpoints = new int[K][K];
for (int n = 0; n < K; n++) {
kpoints[n] = pt.get(combinationArr[n]);
}
return kpoints;
}
}
答案 6 :(得分:0)
使用LINQ的C#解决方案
public int[][] KClosest(int[][] points, int K) {
var orderedPoints = points.OrderBy(point => point[0]*point[0] + point[1]*point[1]);
return orderedPoints.Take(K).ToArray();
}
答案 7 :(得分:-1)
以下方法有什么问题?
1)计算从给定点到其他点的距离。
2)将该点的距离和索引存储到observeEvent(input$action, {
output$text_output <- renderText({
if(input$text %in% "sar"){
"Yes, it is sarcastic"
}
else{
"No, it is not sarcastic"
}
})
})
3)从地图中选择前K个元素。它的值将从点数组中给出Point元素的索引。
地图根据其键的自然顺序或地图创建时提供的比较器进行排序,
答案 8 :(得分:-2)
public static void NearestPoints() {
int[][] Points = { new int[] { -16, 5 },
new int[] { -1, 2 },
new int[] { 4, 3 },
new int[] { 10, -2 },
new int[] { 0, 3 },
new int[] {- 5, -9 } };
// Linq Order by default use quick sort which will be best suited in this.
var orderPoint = from i in Enumerable.Range(0, Points.Length)
orderby Math.Sqrt( Points[i][0] * Points[i][0]
+ Points[i][1] * Points[i][1] )
select new int[][] { Points[i] };
var result = orderPoint.Take(3);
}