Kth Smallest Element in a Sorted Matrix ...simple approach using max heap..faster then 95.6%
104

'''
class Solution {
public:
int kthSmallest(vector<vector>& matrix, int k) {
priority_queuep;
int l=0,i,j,flag=0;
for(i=0;i<matrix.size();i++)
{
for(j=0;j<matrix[i].size();j++)
{
p.push(matrix[i][j]);
l++;
if(k==l)
{
flag=1;
break;
}
}
if(k==l)
break;
}
j++;
// cout<<i<<j<<endl;
while(i<matrix.size())
{
while(j<matrix[i].size())
{
if(matrix[i][j]<p.top())
{
// cout<<p.top()<<" "<<matrix[i][j]<<endl;
p.pop();
p.push(matrix[i][j]);
}
j++;
}
j=0;
i++;
}
return p.top();
}
};
'''

Comments (0)