1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> (nums1[a[0]] + nums2[a[1]]) - (nums1[b[0]] + nums2[b[1]])); for (int i = 0; i < Math.min(nums1.length, k); ++i) { q.offer(new int[]{i, 0}); } List<List<Integer>> ans = new ArrayList<>(); while (k-- > 0 && !q.isEmpty()) { int[] idx = q.poll(); ans.add(List.of(nums1[idx[0]], nums2[idx[1]])); if (++idx[1] < nums2.length) q.offer(idx); } return ans; } }
|