-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path973.cpp
More file actions
22 lines (22 loc) · 679 Bytes
/
973.cpp
File metadata and controls
22 lines (22 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
typedef pair<int, pair<int, int>> P;
int distance(int x, int y) {
return x * x + y * y;
}
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
priority_queue<P> pq;
for (auto point : points) {
int dist = distance(point[0], point[1]);
pq.push(make_pair(dist, make_pair(point[0], point[1])));
while (pq.size() > k) pq.pop();
}
vector<vector<int>> res;
while (!pq.empty()) {
P element = pq.top();
pq.pop();
res.push_back({element.second.first, element.second.second});
}
return res;
}
};