Skip to content

Instantly share code, notes, and snippets.

@SuryaPratapK
Created February 1, 2024 09:18
Show Gist options
  • Save SuryaPratapK/8c571bf30a6eb231d5d4c6874ad5c6d9 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/8c571bf30a6eb231d5d4c6874ad5c6d9 to your computer and use it in GitHub Desktop.
class Solution {
int count(vector<vector<int>>& matrix,int val){
int c=0,n=matrix.size(),lb;
for(int i=0;i<n;++i){
lb = upper_bound(matrix[i].begin(),matrix[i].end(),val)-matrix[i].begin();
c += lb;
}
return c;
}
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
int n=matrix.size();
int low=matrix[0][0],high=matrix[n-1][n-1],mid;
while(low < high){
mid = low+(high-low)/2;
int c = count(matrix,mid);
if(c < k) low=mid+1;
else high=mid;
}
return low;
}
};
@AjaySh1
Copy link

AjaySh1 commented May 16, 2024

good solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment