Find K nearest Points to Point P in 2-dimensional plane

后端 未结 9 1800
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 04:27

Source: AMAZON INTERVIEW QUESTION

Given a point P and other N points in two dimensional space, find K points

9条回答
  •  没有蜡笔的小新
    2021-01-31 04:48

    C# Solution using LINQ

    public int[][] KClosest(int[][] points, int[][] p, int K) {
    
        var orderedPoints = points.OrderBy(point => Math.Pow(point[0]-p[0], 2) + Math.Pow(point[1]-p[1], 2));
        return orderedPoints.Take(K).ToArray();
    }
    

提交回复
热议问题