|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.Collections;
|
5 | 5 | import java.util.List;
|
6 |
| -/**378. Kth Smallest Element in a Sorted Matrix |
7 |
| - * |
8 |
| -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. |
| 6 | + |
| 7 | +/** |
| 8 | + * 378. Kth Smallest Element in a Sorted Matrix |
| 9 | + * Given a n x n matrix where each of the rows and columns are |
| 10 | + * sorted in ascending order, find the kth smallest element in the matrix. |
9 | 11 |
|
10 | 12 | Note that it is the kth smallest element in the sorted order, not the kth distinct element.
|
11 | 13 |
|
|
20 | 22 |
|
21 | 23 | return 13.
|
22 | 24 |
|
23 |
| - Note: |
24 |
| -You may assume k is always valid, 1 ≤ k ≤ n2.*/ |
| 25 | +Note: |
| 26 | +You may assume k is always valid, 1 ≤ k ≤ n2. |
| 27 | + */ |
| 28 | + |
25 | 29 | public class _378 {
|
26 |
| - //brute force made it AC'ed, extreme test case needed for OJ |
27 |
| - public int kthSmallest(int[][] matrix, int k) { |
28 |
| - List<Integer> list = new ArrayList<Integer>(); |
29 |
| - int n = matrix.length; |
30 |
| - for (int i = 0; i < n; i++) { |
31 |
| - for (int j = 0; j < n; j++) { |
32 |
| - list.add(matrix[i][j]); |
| 30 | + public static class Solution1 { |
| 31 | + /** |
| 32 | + * brute force made it AC'ed, extreme test case needed for OJ |
| 33 | + */ |
| 34 | + public int kthSmallest(int[][] matrix, int k) { |
| 35 | + List<Integer> list = new ArrayList(); |
| 36 | + int n = matrix.length; |
| 37 | + for (int i = 0; i < n; i++) { |
| 38 | + for (int j = 0; j < n; j++) { |
| 39 | + list.add(matrix[i][j]); |
| 40 | + } |
33 | 41 | }
|
| 42 | + Collections.sort(list); |
| 43 | + return list.get(k - 1); |
34 | 44 | }
|
35 |
| - Collections.sort(list); |
36 |
| - return list.get(k - 1); |
37 | 45 | }
|
38 | 46 |
|
39 |
| - //TODO: use heap and binary search to do it. |
40 |
| - |
41 |
| - //Binary Search : The idea is to pick a mid number than compare it with the elements in each row, we start form |
42 |
| - // 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 |
43 |
| - // that less than mid and compare with k, then update the k. |
44 |
| - public int kthSmallestBS(int[][] matrix, int k) { |
45 |
| - int row = matrix.length - 1; |
46 |
| - int col = matrix[0].length - 1; |
47 |
| - int lo = matrix[0][0]; |
48 |
| - int hi = matrix[row][col]; |
49 |
| - while (lo < hi) { |
50 |
| - int mid = lo + (hi - lo) / 2; |
51 |
| - int count = 0; |
52 |
| - int j = col; |
53 |
| - for (int i = 0; i <= row; i++) { |
54 |
| - while (j >= 0 && matrix[i][j] > mid) { |
55 |
| - j--; |
| 47 | + public static class Solution2 { |
| 48 | + /** |
| 49 | + * Binary Search : The idea is to pick a mid number, then compare it with the elements in each row, we start form |
| 50 | + * 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 |
| 51 | + * that less than mid and compare with k, then update the k. |
| 52 | + */ |
| 53 | + public int kthSmallestBS(int[][] matrix, int k) { |
| 54 | + int row = matrix.length - 1; |
| 55 | + int col = matrix[0].length - 1; |
| 56 | + int lo = matrix[0][0]; |
| 57 | + int hi = matrix[row][col]; |
| 58 | + while (lo < hi) { |
| 59 | + int mid = lo + (hi - lo) / 2; |
| 60 | + int count = 0; |
| 61 | + int j = col; |
| 62 | + for (int i = 0; i <= row; i++) { |
| 63 | + while (j >= 0 && matrix[i][j] > mid) { |
| 64 | + j--; |
| 65 | + } |
| 66 | + count += (j + 1); |
| 67 | + } |
| 68 | + if (count < k) { |
| 69 | + lo = mid + 1; |
| 70 | + } else { |
| 71 | + hi = mid; |
56 | 72 | }
|
57 |
| - count += (j + 1); |
58 |
| - } |
59 |
| - if (count < k) { |
60 |
| - lo = mid + 1; |
61 |
| - } else { |
62 |
| - hi = mid; |
63 | 73 | }
|
| 74 | + return lo; |
64 | 75 | }
|
65 |
| - return lo; |
66 | 76 | }
|
67 | 77 | }
|
0 commit comments