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
| class Solution { public int kthSmallest(int[][] mat, int k) { var a = new int[]{0}; for (var row : mat) a = kSmallestPairs(row, a, k); return a[k - 1]; }
private int[] kSmallestPairs(int[] nums1, int[] nums2, int k) { int n = nums1.length, m = nums2.length, sz = 0; var ans = new int[Math.min(k, n * m)]; var pq = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]); pq.add(new int[]{nums1[0] + nums2[0], 0, 0}); while (!pq.isEmpty() && sz < k) { var p = pq.poll(); int i = p[1], j = p[2]; ans[sz++] = nums1[i] + nums2[j]; if (j == 0 && i + 1 < n) pq.add(new int[]{nums1[i + 1] + nums2[0], i + 1, 0}); if (j + 1 < m) pq.add(new int[]{nums1[i] + nums2[j + 1], i, j + 1}); } return ans; } }
|