class Solution { public: #define fi first #define se second bool comp(vector<int> a,…
- User
class Solution {
public: #define fi first #define se second
bool comp(vector<int> a, pair<int, int> b) { return a[0] * a[0] + a[1] * a[1] < b.fi * b.fi + b.se * b.se; } vector<vector<int>> kClosest(vector<vector<int>>& points, int k) { bool compare = [](vector<int>& a, vector<int>& b) { return a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1]; }; priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq; for (auto i : points) { if (pq.size() < k) { pq.push({i[0], i[1]}); } else { if (comp(i, pq.top())) { pq.pop(); pq.push({i[0], i[1]}); } } } vector<vector<int>> ans; // while (pq.size()) { // auto i = pq.top(); // ans.push_back({i.fi, i.se}); // pq.pop(); // } return ans; }}; find bug
- Assistant
The code you've provided to find the
kclosest points to the origin has a few issues, but mainly, the function to compare points and collect the results is not implemented correctly. Here is a breakdown of the issues: