How the time complexity can be optimised?

here the time complexity we had achieved is O(klog n)
but how can we optimise it to O(k
log k)

Hey @namangarg31
This klogk solution

int kthSmallest(vector<vector>& matrix, int k) {
int n=matrix.size();
set<pair<int,pair<int,int>>> s;
s.insert({matrix[0][0],{0,0}});
k–;
while(k–){
pair<int,pair<int,int>> p=*s.begin();
s.erase(s.begin());
int val=p.first;
int row=p.second.first;
int col=p.second.second;
if(row+1<n)
s.insert({matrix[row+1][col],{row+1,col}});
if(col+1<n)
s.insert({matrix[row][col+1],{row,col+1}});
}
return s.begin()->first;
}