forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0973-k-closest-points-to-origin.c
48 lines (38 loc) · 1.31 KB
/
0973-k-closest-points-to-origin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
typedef struct {
int x;
int y;
} Point;
// Function to calculate the Euclidean distance from origin
double distance(Point p) {
return sqrt(p.x * p.x + p.y * p.y);
}
// Function to compare two points based on distance
int compare(const void* a, const void* b) {
Point* pointA = (Point*)a;
Point* pointB = (Point*)b;
double distanceA = distance(*pointA);
double distanceB = distance(*pointB);
if (distanceA < distanceB) return -1;
if (distanceA > distanceB) return 1;
return 0;
}
// Function to find k closest points to origin
int** kClosest(int** points, int pointsSize, int* pointsColSize, int k, int* returnSize, int** returnColumnSizes) {
Point* pointArr = (Point*)malloc(pointsSize * sizeof(Point));
for (int i = 0; i < pointsSize; i++) {
pointArr[i].x = points[i][0];
pointArr[i].y = points[i][1];
}
qsort(pointArr, pointsSize, sizeof(Point), compare);
int** result = (int**)malloc(k * sizeof(int*));
*returnSize = k;
*returnColumnSizes = (int*)malloc(k * sizeof(int));
for (int i = 0; i < k; i++) {
result[i] = (int*)malloc(2 * sizeof(int));
result[i][0] = pointArr[i].x;
result[i][1] = pointArr[i].y;
(*returnColumnSizes)[i] = 2;
}
free(pointArr);
return result;
}