Untitled

 avatar
unknown
plain_text
a year ago
566 B
9
Indexable
vector<vector<int>> kClosest(vector<int> A, vector<int> B,int n, int k)
    {
        priority_queue<vector<int>> maxHeap;
        for (int i=0;i<n;++i) {
            int x = A[i], y = B[i];
            maxHeap.push({x*x + y*y, x, y});
            if (maxHeap.size() > k) {
                maxHeap.pop();
            }
        }
        vector<vector<int>> ans(k);
        for (int i = 0; i < k; ++i) {
            vector<int> top = maxHeap.top();
            maxHeap.pop();
            ans[i] = {top[1], top[2]};
        }
        return ans;
    }
Editor is loading...
Leave a Comment