We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1bae725 commit c94329eCopy full SHA for c94329e
solution/0900-0999/0973.K Closest Points to Origin/Solution2.cpp
@@ -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
16
17
+ return ans;
18
19
+};
0 commit comments