Skip to content

Commit c94329e

Browse files
authored
Create Solution2.cpp
1 parent 1bae725 commit c94329e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
4+
priority_queue<pair<double, int>> pq;
5+
for (int i = 0, n = points.size(); i < n; ++i) {
6+
double dist = hypot(points[i][0], points[i][1]);
7+
pq.push({dist, i});
8+
if (pq.size() > k) {
9+
pq.pop();
10+
}
11+
}
12+
vector<vector<int>> ans;
13+
while (!pq.empty()) {
14+
ans.push_back(points[pq.top().second]);
15+
pq.pop();
16+
}
17+
return ans;
18+
}
19+
};

0 commit comments

Comments
 (0)