public int[][] KClosest(int[][] points, int K)
        {
            int[] distanceArray = new int[points.Length];
            for (var x = 0; x < points.Length; x++)
            {
                distanceArray[x] = EuclideanDistanceFromOrigin(points[x][0], points[x][1]);
            }

            QuickSelectOnOwn QuickSelectOnOwn = new QuickSelectOnOwn();
            var ksmallest = QuickSelectOnOwn.KthSmallest(distanceArray, 0, points.Length - 1, K - 1);

            int[][] answer = new int[K][];
            var     kcount = K;

            for (var x = 0; x < points.Length; x++)
            {
                var distance = EuclideanDistanceFromOrigin(points[x][0], points[x][1]);
                if (distance <= ksmallest)
                {
                    answer[kcount - 1] = new int [] { points[x][0], points[x][1] };
                    kcount--;
                }
            }

            return(answer);
        }
Ejemplo n.º 2
0
 public void KthSmallest()
 {
     int[]            arr    = { 12, 11, 13, 13, 13, 7 };
     int              n      = arr.Length;
     int              k      = 2;
     QuickSelectOnOwn qs     = new QuickSelectOnOwn();
     var              answer = qs.KthSmallest(arr, 0, n - 1, k - 1);
 }