19 lines code with rb-tree


  • 0
    F
    class Solution {
    public:
        int kthSmallest(vector<vector<int>>& matrix, int k) {
            multiset<int> my_set;
            for (auto vi : matrix) {
                for (auto vj : vi) {
                    my_set.emplace(vj);
                }
            }
            int result = 0;
            for (auto l : my_set) {
                if (k == 1) {
                    result = l;
                }
                -- k;
            }
            return result;
        }
    };
    

    std::multiset is implement by rb_tree. So these element in the tree would be sorted by key, which is exactly what we want.


Log in to reply
 

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