forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKthSmallestElementinaSortedMatrix.java
53 lines (43 loc) · 1.15 KB
/
KthSmallestElementinaSortedMatrix.java
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
43
44
45
46
47
48
49
50
51
52
53
class Solution {
// Solution One TC : O(nlogk)
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> (b - a));
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
pq.offer(matrix[i][j]);
if (pq.size() > k) {
pq.poll();
}
}
}
return pq.peek();
}
// Solution 2 using Binary Search
public int kthSmallest(int[][] matrix, int k) {
int n = matrix.length, lo = matrix[0][0], hi = matrix[n - 1][n - 1];
// lo < hi , not lo <= hit
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
int count = lessEqual(matrix, mid);
if (count < k)
lo = mid + 1;
//
else
hi = mid;
}
return lo;
}
// from left-bottom or right-top can count how much numbers are less equal than target
public int lessEqual(int[][] matrix, int target) {
int cnt = 0, N = matrix.length, i = N - 1, j = 0;
while (i >= 0 && j < N) {
if (matrix[i][j] > target)
i--;
else {
cnt = cnt + i + 1;
j++;
}
}
return cnt;
}
}