Skip to content

Commit

Permalink
Update 0973-k-closest-points-to-origin.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
kiryl-labada authored Jan 7, 2023
1 parent f1f6e7b commit db263fc
Showing 1 changed file with 16 additions and 39 deletions.
55 changes: 16 additions & 39 deletions csharp/0973-k-closest-points-to-origin.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
public class Solution {
// Closest to origin minHeap
private PriorityQueue<int[], double> pq;
private int size;
// T: O(Max(M, KLogM)), S: O(k)
public int[][] KClosest(int[][] points, int k) {
pq = new PriorityQueue<int[], double>();
size = k;

AddToPriorityQueue(points);

return Closest();
}

public class MaxHeap : IComparer<double>{
public int Compare(double x, double y){
if( x< y) return 1;å
else if (x > y) return -1;
else return 0;
}
}

// T: O(M)
private void AddToPriorityQueue(int[][] points){
foreach(var point in points){
//var value = (double) Math.Sqrt(point[0]*point[0] + point[1]*point[1]);
var value = (double) point[0]*point[0] + point[1]*point[1];
pq.Enqueue(point, value);


}
}

// T: O(KLogM)
private int[][] Closest(){
var result = new List<int[]>();
while(size > 0){
result.Add(pq.Dequeue());
size--;
var items = points.Select(point => {
long x = point[0];
long y = point[1];

return (point, x * x + y * y);
});

int[][] result = new int[k][];
// T: O(n)
PriorityQueue<int[], long> queue = new(items);

// T: O(k log(n))
for (int i = 0; i < k; i++) {
result[i] = queue.Dequeue();
}
return result.ToArray();

return result;
}
}

0 comments on commit db263fc

Please sign in to comment.