Advertisement
1988coder

378. Kth Smallest Element in a Sorted Matrix

Jan 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
  3. import java.util.PriorityQueue;
  4.  
  5. /**
  6.  * Using PriorityQueue
  7.  *
  8.  * Time Complexity: O(A log A + K log K)
  9.  *
  10.  * Space Complexity: O(A)
  11.  *
  12.  * A = min(N, K). N = size of matrix. K = input value k.
  13.  */
  14. class Solution1 {
  15.     public int kthSmallest(int[][] matrix, int k) throws IllegalArgumentException {
  16.         if (matrix == null || k <= 0 || matrix.length == 0 || matrix[0].length == 0) {
  17.             throw new IllegalArgumentException("Illegal Input");
  18.         }
  19.  
  20.         int n = matrix.length;
  21.  
  22.         if (k > n * n) {
  23.             throw new IllegalArgumentException("k is greater than the number of elements in the matrix.");
  24.         }
  25.         if (k == 1) {
  26.             return matrix[0][0];
  27.         }
  28.         if (k == n * n) {
  29.             return matrix[n - 1][n - 1];
  30.         }
  31.  
  32.         PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> (matrix[a[0]][a[1]] - matrix[b[0]][b[1]]));
  33.  
  34.         for (int i = 0; i < Math.min(n, k); i++) {
  35.             queue.offer(new int[] { i, 0 });
  36.         }
  37.         for (int i = 0; i < k - 1; i++) {
  38.             int[] cell = queue.poll();
  39.             if (cell[1] == n - 1) {
  40.                 continue;
  41.             }
  42.             queue.offer(new int[] { cell[0], cell[1] + 1 });
  43.         }
  44.  
  45.         int[] res = queue.poll();
  46.         return matrix[res[0]][res[1]];
  47.     }
  48. }
  49.  
  50. /**
  51.  * Using Binary Search
  52.  *
  53.  * Refer: 1)
  54.  * https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173/Share-my-thoughts-and-Clean-Java-Code
  55.  * 2)
  56.  * https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173/Share-my-thoughts-and-Clean-Java-Code/122873
  57.  *
  58.  * Time Complexity: O(N^2 * log(N^2)) = O(N^2 * log N)
  59.  *
  60.  * Space Complexity: O(1)
  61.  *
  62.  * N = size of matrix.
  63.  */
  64. class Solution2 {
  65.     public int kthSmallest(int[][] matrix, int k) throws IllegalArgumentException {
  66.         if (matrix == null || matrix.length == 0 || matrix[0].length == 0 || k <= 0) {
  67.             throw new IllegalArgumentException("Illegal Input");
  68.         }
  69.  
  70.         int n = matrix.length;
  71.  
  72.         if (k > n * n) {
  73.             throw new IllegalArgumentException("k is greater than the number of elements in the matrix.");
  74.         }
  75.         if (k == 1) {
  76.             return matrix[0][0];
  77.         }
  78.         if (k == n * n) {
  79.             return matrix[n - 1][n - 1];
  80.         }
  81.  
  82.         int low = matrix[0][0];
  83.         int high = matrix[n - 1][n - 1];
  84.  
  85.         while (low < high) {
  86.             int mid = low + (high - low) / 2;
  87.             int count = 0;
  88.             int j = n - 1;
  89.             for (int i = 0; i < n; i++) {
  90.                 while (j >= 0 && matrix[i][j] > mid) {
  91.                     j--;
  92.                 }
  93.                 count += (j + 1);
  94.             }
  95.             if (count < k) {
  96.                 low = mid + 1;
  97.             } else {
  98.                 high = mid;
  99.             }
  100.         }
  101.  
  102.         return low;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement