




Sponsored
Sponsored
This approach leverages the simplicity of sorting the list of points based on their distance from the origin. After sorting, the first k points will be the closest ones. The key is to use the squared Euclidean distance to avoid the computational overhead of square root operations.
Time Complexity: O(n log n) due to sorting. 
Space Complexity: O(1) since the sorting is done in-place.
1using System;
2using System.Linq;
3
4public class Solution {
5    public int[][] KClosest(int[][] points, int k) {
6        return points.OrderBy(point => point[0] * point[0] + point[1] * point[1]).Take(k).ToArray();
7    }
8}
9This C# solution uses LINQ's OrderBy to sort points by squared distance, followed by Take to extract the first k elements.
The Max-Heap approach uses a priority queue to maintain the k closest points seen so far. By using a max-heap, we can efficiently insert new points and potentially evict the farthest point if it is further than any encountered point, leading to a reduced time complexity for finding the k closest points.
Time Complexity: O(n log k) since each insertion/extraction in the heap takes O(log k) time. 
Space Complexity: O(k) for the heap storage.
1
This C solution maintains a max-heap of size k using a custom struct. It inserts each point by distance until the max-heap is full. For subsequent points, it compares distances and potentially evicts the farthest point if a closer point is found.