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.
class Solution { public int kthSmallest(int[][] m, int k) { int n = m.length; PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { pq.offer(m[i][j]); if(pq.size() > k) pq.poll(); } } return pq.poll(); } }
用maxheap(pq)维持一个size为k的pq
class Solution { public int kthSmallest(int[][] matrix, int k) { // num of rows and cols in matrix int rows = matrix.length, cols = matrix[0].length; // get the lowest and highest possible num, will shrink search space according to the two nums // [lo, hi] is our initial search range int lo = matrix[0][0], hi = matrix[rows - 1][cols - 1] ; while(lo <= hi) { int mid = lo + (hi - lo) / 2; int count = 0, maxNum = lo; // for each row, we r going to find # of nums < mid in that row for (int r = 0, c = cols - 1; r < rows; r++) { while (c >= 0 && matrix[r][c] > mid) c--; // this row's c has to be smaller than the c found in last row due to the sorted property of the matrix if (c >= 0) { count += (c + 1); // count of nums <= mid in matrix maxNum = Math.max(maxNum, matrix[r][c]); // mid might be value not in matrix, we need to record the actually max num; } } // adjust search range System.out.println(lo + " " + hi + " " + mid + " " + count); if (count == k) return maxNum; else if (count < k) lo = mid + 1; else hi = mid - 1; } // 1) Q: Why we return lo at the end: // A: Here lo=hi+1, for hi, we found <k elems, for lo, we found >=k elem, lo must have duplicates in matrix, return lo // 2) Q: Why lo exist in the matrix // A: for lo which is only 1 more than hi, we could find some extra nums in matrix so that there are >=k elems, so lo it self must exist in the matrix to meet the requirement. please check the illustration at the beginning of the post return lo; } }
https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173/Share-my-thoughts-and-Clean-Java-Code

浙公网安备 33010602011771号