简体   繁体   中英

How to push pair from priority queue into a 2D array?

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.

// 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM