5 lines C++ MinHeap and multiset


  • 0

    Solution 1. MinHeap

    class Solution {
    public:
        int kthSmallest(vector<vector<int>>& matrix, int k) {
            priority_queue<int, vector<int>, greater<int>>pq;
            for(auto x: matrix)
                for(auto y: x) pq.push(y);
            while(--k) pq.pop();
            return pq.top();
        }
    };
    

    Solution 2. Multiset

    class Solution {
    public:
        int kthSmallest(vector<vector<int>>& matrix, int k) {
            multiset<int>s;
            for(auto x: matrix)
                for(auto y: x) s.insert(y);
            auto p = s.begin();
            while(--k) p++;
            return *p;
        }
    };
    

Log in to reply
 

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