forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_378.java
65 lines (57 loc) · 1.94 KB
/
_378.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
54
55
56
57
58
59
60
61
62
63
64
65
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**378. Kth Smallest Element in a Sorted Matrix
*
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.*/
public class _378 {
//brute force made it AC'ed, extreme test case needed for OJ
public int kthSmallest(int[][] matrix, int k) {
List<Integer> list = new ArrayList<Integer>();
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
list.add(matrix[i][j]);
}
}
Collections.sort(list);
return list.get(k - 1);
}
//TODO: use heap and binary search to do it.
//Binary Search : The idea is to pick a mid number than compare it with the elements in each row, we start form
// end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements
// that less than mid and compare with k, then update the k.
public int kthSmallestBS(int[][] matrix, int k) {
int row = matrix.length - 1, col = matrix[0].length - 1;
int lo = matrix[0][0];
int hi = matrix[row][col];
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
int count = 0, j = col;
for (int i = 0; i <= row; i++) {
while (j >= 0 && matrix[i][j] > mid) {
j--;
}
count += (j + 1);
}
if (count < k) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo;
}
}