class Solution { public: vector<vector<int>> kClosest(vector<vector<int>>& points, int k) { int n=points.size(); priority_queue<pair<int,pair<int, int>>> mxH; for(int i=0;i<n;i++) { int dis=pow(points[i][0],2)+pow(points[i][1],2); mxH.push({dis,{points[i][0], points[i][1]}}); if(mxH.size()>k) mxH.pop(); } vector<vector<int>> ans; while(!mxH.empty()) { vector<int> temp; temp.push_back(mxH.top().second.first); temp.push_back(mxH.top().second.second); ans.push_back(temp); mxH.pop(); } return ans; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter