-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestElementInSortedMatrix.java
More file actions
42 lines (35 loc) · 1.15 KB
/
KthSmallestElementInSortedMatrix.java
File metadata and controls
42 lines (35 loc) · 1.15 KB
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
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int lo = matrix[0][0],
hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1;//[lo, hi)
while(lo < hi) {
int mid = lo + (hi - lo) / 2;
int count = 0,
j = matrix[0].length - 1;
for(int i = 0; i < matrix.length; i++) {
while(j >= 0 && matrix[i][j] > mid)
j--;
count += (j + 1);
}
if(count < k)
lo = mid + 1;
else
hi = mid;
}
return lo;
}
public int kthSmallestPassesPartialCases(int[][] matrix, int k) {
int dup = 0;
TreeSet<Integer> set = new TreeSet<>();
for(int i=0; i<matrix.length; i++)
for(int j=0; j<matrix[0].length; j++){
if(!set.contains(matrix[i][j]))
set.add(matrix[i][j]);
else
dup++;
}
for(int i=0; i<k-dup-1; i++)
set.pollFirst();
return set.first();
}
}