Skip to content

feat: add ts solution to lc problem: No.0973 #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jul 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create Solution2.cpp
  • Loading branch information
yanglbme authored Jul 2, 2024
commit c94329e28244a77beb357c5dbc21eb0d6afd9d62
19 changes: 19 additions & 0 deletions solution/0900-0999/0973.K Closest Points to Origin/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
priority_queue<pair<double, int>> pq;
for (int i = 0, n = points.size(); i < n; ++i) {
double dist = hypot(points[i][0], points[i][1]);
pq.push({dist, i});
if (pq.size() > k) {
pq.pop();
}
}
vector<vector<int>> ans;
while (!pq.empty()) {
ans.push_back(points[pq.top().second]);
pq.pop();
}
return ans;
}
};
Loading