Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define pv pair<int,pair<int,int>>
- class Solution {
- public:
- vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
- priority_queue<pv,vector<pv>,greater<pv>> pq; //find distnace of all nodes from origin or other point if given as source
- for(int i=0;i<points.size();i++){ //and push it into min heap along with it's cordinates
- int a=points[i][0],b=points[i][1];
- pq.push(make_pair(a*a+b*b,make_pair(a,b)));
- }
- vector<vector<int>> res;
- for(int i=0;i<k;i++){ //pop k cordinates from min_heap which will be closest to origin
- pv temp=pq.top();
- pq.pop();
- vector <int> t;
- t.push_back(temp.second.first);
- t.push_back(temp.second.second);
- res.push_back(t);
- t.clear();
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement