378. Kth Smallest Element in a Sorted Matrix (Brute Force)

class Solution { public: int kthSmallest(vector<vector<int>>& matrix, int k) { priority_queue<int, vector<int>, greater<int> > pq; for (auto & row : matrix){ for (auto & e: row) pq.push(e); } for (int i = 0; i < k-1; i++) pq.pop(); return pq.top(); } };
Note here how many times you pop... you should pop (k-2) times...

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.