class Solution {
private int[] arr1, arr2;
public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
arr1 = nums1;
arr2 = nums2;
List<List<Integer>> ans = new ArrayList<>(k);
PriorityQueue<int[]> Q = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return (arr1[o1[0]] + arr2[o1[1]]) - (arr1[o2[0]] + arr2[o2[1]]);
}
});
for (int i = 0; i < arr1.length; i++) {
Q.add(new int[]{i, 0});
}
for (int i = 0; i < k; i++) {
int[] poll = Q.poll();
ans.add(Arrays.asList(arr1[poll[0]], arr2[poll[1]]));
if (poll[1] + 1 < arr2.length) {
Q.add(new int[]{poll[0], poll[1] + 1});
}
}
return ans;
}
}