-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKInMartix.java
More file actions
31 lines (25 loc) · 785 Bytes
/
KInMartix.java
File metadata and controls
31 lines (25 loc) · 785 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
public class KInMartix {
public static void main(String[] args){
int[][] martix = new int[][]{{1,5,9},{10,11,13},{12,13,15}};
System.out.println(sort(martix,8));
}
public static int sort(int[][] martix, int k) {
int n = martix[0].length;
int low = martix[0][0],high = martix[n-1][n-1];
while (low < high) {
int mid = low + (high - low) / 2;
int count = 0;
int j = martix[0].length -1;
for(int i = 0; i < n ; i++) {
while (j >= 0 && martix[i][j] > mid) j--;
count += j + 1;
}
if(count < k) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}