-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path378.java
More file actions
31 lines (29 loc) · 811 Bytes
/
378.java
File metadata and controls
31 lines (29 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int kthSmallest(int[][] mat, int k) {
int n = mat.length;
int m = mat[0].length;
int low = mat[0][0], high = mat[n-1][m-1];
while(low < high){
int mid = low + (high-low)/2;
int count = countSmaller(n, m, mid, mat);
if(count < k){
low = mid + 1;
}else{
high = mid;
}
}
return low;
}
int countSmaller(int n, int m, int target, int[][] mat){
int count = 0;
for(int j=m-1,i=0; i<n && j>=0; i++){
if(mat[i][j] > target){
i--;
j--;
}else{
count += j+1;
}
}
return count;
}
}