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

后端 未结 9 1727
佛祖请我去吃肉
佛祖请我去吃肉 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:55

    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);
    }
    

提交回复
热议问题