I can’t find this question ?? where is this ?
Kth Smallest Element in Row
It is not there on hackerblocks.
You can practice here https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
class Solution {
public:
int kthSmallest(vector<vector>& matrix, int k) {
priority_queue pq;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix.size();j++){
if(pq.size()<k){
pq.push(matrix[i][j]);
}else{
if(matrix[i][j]<pq.top()){
pq.pop();
pq.push(matrix[i][j]);
}
}
}
}
return pq.top();
}
};
I have used maxHeap logic here in this question. Is this efficient approach??
Yes your approach is correct and efficient
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.