-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0973-k-closest-points-to-origin.cpp
111 lines (99 loc) · 3.17 KB
/
0973-k-closest-points-to-origin.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Given array of points & an int k, return k closest points to (0, 0)
Ex. points = [[1,3],[-2,2]], k = 1 -> [[-2,2]]
Quickselect, partition until pivot = k, left side all < k
Time: O(k log n)
Space: O(n)
*/
// class Solution {
// public:
// vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
// priority_queue<pair<double, vector<int>>> pq;
// for (int i = 0; i < points.size(); i++) {
// double distance = sqrt(pow(points[i][0], 2) + pow(points[i][1], 2));
// pq.push({distance, points[i]});
// if (pq.size() > k) {
// pq.pop();
// }
// }
// vector<vector<int>> result;
// while(!pq.empty()) {
// result.push_back(pq.top().second);
// pq.pop();
// }
// return result;
// }
// };
/*
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
int low = 0;
int high = points.size() - 1;
int pivotIndex = points.size();
while (pivotIndex != k) {
pivotIndex = partition(points, low, high);
if (pivotIndex < k) {
low = pivotIndex;
} else {
high = pivotIndex - 1;
}
}
return vector<vector<int>>(points.begin(), points.begin() + k);
}
private:
int partition(vector<vector<int>>& points, int low, int high) {
vector<int> pivot = points[low + (high - low) / 2];
int pivotDistance = getDistance(pivot);
while (low < high) {
if (getDistance(points[low]) >= pivotDistance) {
swap(points[low], points[high]);
high--;
} else {
low++;
}
}
if (getDistance(points[low]) < pivotDistance) {
low++;
}
return low;
}
int getDistance(vector<int>& point) {
return pow(point[0], 2) + pow(point[1], 2);
}
};
*/
/*
// O(n logn) solution using sorting
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
vector<vector<int>> res(k);
sort(points.begin(), points.end(), [](vector<int>& p1, vector<int>& p2){
int dist_p1 = pow(p1[0],2) + pow(p1[1],2);
int dist_p2 = pow(p2[0],2) + pow(p2[1],2);
return dist_p1 < dist_p2;
});
copy(points.begin(), points.begin() + k, res.begin());
return res;
}
};
*/
// O(k logn) solution
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
vector<vector<int>> triples;
for (auto& p : points)
triples.push_back({p[0] * p[0] + p[1] * p[1], p[0], p[1]});
// Min heap of vectors (triples). This constructor takes O(n) time (n = len(v))
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq(triples.begin(), triples.end());
vector<vector<int>> res;
while (k--){
vector<int> el = pq.top();
pq.pop();
res.push_back({el[1], el[2]});
}
return res;
}
};