-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path973-k-closest-points-to-origin.cpp
34 lines (32 loc) · 1.17 KB
/
973-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
// Two heaps based solution. Doesn't run very fast
// Still need to solve it using divide and conquer
#include <queue>
class Solution {
public:
double distance_squared(vector<int> a) {
return a[0]*a[0] + a[1]*a[1];
}
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
priority_queue <vector<int>, vector<vector<int>>, function <bool(vector<int>, vector<int>)> >
max_heap([](vector<int> a, vector<int> b) {
return a[0]*a[0] + a[1]*a[1] < b[0]*b[0] + b[1]*b[1];
});
max_heap.emplace(points[0]);
for (int i = 1; i < points.size(); i++) {
if (max_heap.size() < K) {
max_heap.emplace(points[i]);
} else {
if (distance_squared(points[i]) < distance_squared(max_heap.top())) {
max_heap.pop();
max_heap.emplace(points[i]);
}
}
}
vector<vector<int>> result = {};
while (!empty(max_heap)) {
result.push_back(max_heap.top());
max_heap.pop();
}
return {rbegin(result), rend(result)};
}
};