Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
566 B
6
Indexable
Never
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;
    }
Leave a Comment