Created
February 1, 2024 09:18
-
-
Save SuryaPratapK/8c571bf30a6eb231d5d4c6874ad5c6d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good solution