c++ priority queue


  • 0
    P
    typedef std::pair<int, std::pair<int, int>> pp;
    
    class Solution {
    public:
    
    int kthSmallest(std::vector<std::vector<int>>& matrix, int k){
        std::priority_queue<pp, std::vector<pp>, std::greater<pp>> pq;
        pq.push({matrix[0][0], {0, 0}});
        pp p;
        while(k--){
            p = pq.top();
            pq.pop();
            auto x = p.second.first, y = p.second.second;
            if (x+1<matrix.size()){
                pq.push({matrix[x+1][y], {x+1, y}});
            }
            if (x==0 && y+1 <matrix[0].size()){
                pq.push({matrix[x][y+1], {x, y+1}});
            }
        }
        return p.first;
    }
    `
    
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.