1

I am doing a leetcode problem, and I am required to return a 2d array of results. But I am using a priority queue for that and am unable to move elements to a 2d array. I am not able to come up with a syntax for this. The push_back(), is not working when I try to push the pair in a 2d array. here is the link to the problem

Code -


    class Solution {
public:
    vector<vector<int>> kClosest(vector<vector<int>>& p, int k) {
        vector<vector<int>>closest;
        //pairp;
       priority_queue<pair<int,pair<int,int>>>heap;
        
        for(int i = 0; i < p.size(); i++){
            
            heap.push({p[i][0] * p[i][0] + p[i][1]*p[i][1],
                       {p[i][0],p[i][1]}});
                      
                      
        }
        
        if(heap.size() > k){
            heap.pop();
        }
        
        
        while(heap.size() > 0){
            pair<int,int>ptr = heap.top().second;
            //want to add the statement to copy elements to closest[][] here
            heap.pop();
        }
    return closest;
    }

};

Error message on adding, closest.push_back(ptr);

Line 22: Char 21: error: no matching member function for call to 'push_back'

        closest.push_back(ptr);
        ~~~~~~~~^~~~~~~~~

/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1184:7: note: candidate function not viable: no known conversion from 'pair<int, int>' to 'const std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>::value_type' (aka 'const std::vector<int, std::allocator>') for 1st argument push_back(const value_type& __x) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1200:7: note: candidate function not viable: no known conversion from 'pair<int, int>' to 'std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>::value_type' (aka 'std::vector<int, std::allocator>') for 1st argument push_back(value_type&& __x) ^ 1 error was generated.

1
  • closest is a vector of a vector of int. You can't push a pair<int,int> into it as it expects a vector<int>. Maybe you want to convert your pair into a vector, maybe you want something else; it's not really clear from your question. If heap consists of the element {2, {1,0}}, what do you want closest to look like after you inserted that element? Commented Jan 20, 2022 at 11:28

1 Answer 1

0
// if closet should be your answer then its size will be k
/* Here we tell how many rows
    the 2D vector is going to have. And Each row will contain a vector of size 2 */
int row = k;
vector<vector<int>>closest(row, vector<int>(2));
int index = 0;
while(heap.size() > 0) {
    pair<int,int>ptr = heap.top().second;

    //Added [want to add] the statement to copy elements to closest[][] here
    closet[index][0] = ptr.first;
    closet[index][1] = ptr.second;
    index++;

    heap.pop();
}

Also, I would like to correct your code where you are popping out elements. you should check with the while condition instead of the 'if' condition. The 'if' condition will just pop it once. It is as below:

while(heap.size() > k){
    heap.pop();
}

// if you want to use push_back then you can write as below:

// Also correct the code as suggested above with 'if ' condition replaced to 'while'

vector<vector<int>>closest(k);
int index = 0;
while(heap.size() > 0){
    pair<int,int>ptr = heap.top().second;
    closest[index].push_back(ptr.first);
    closest[index].push_back(ptr.second);
    index++;
    heap.pop();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.