Since the arrays are sorted, the smallest sum is always nums1[0] + nums2[0]. We use a Min-Heap to explore potential pairs. We start with pairs (nums1[i], nums2[0]) for all i (up to k). When we pop a pair (nums1[i], nums2[j]), the next candidate from that nums1 element is (nums1[i], nums2[j+1]).
- Initialize a Min-Heap.
- Push
(nums1[i] + nums2[0], i, 0)forifrom 0 tomin(len(nums1), k) - 1. - While
k > 0and heap is not empty:- Pop
(sum, i, j). - Add
[nums1[i], nums2[j]]to result. - If
j + 1 < len(nums2), push(nums1[i] + nums2[j+1], i, j+1)to heap. - Decrement
k.
- Pop
- Return result.
- Time Complexity: O(K log K).
- Space Complexity: O(K).
import heapq
def k_smallest_pairs(nums1, nums2, k):
if not nums1 or not nums2: return []
h = []
# Initial candidates: (sum, index_in_nums1, index_in_nums2)
for i in range(min(len(nums1), k)):
heapq.heappush(h, (nums1[i] + nums2[0], i, 0))
res = []
while h and k > 0:
val, i, j = heapq.heappop(h)
res.append([nums1[i], nums2[j]])
if j + 1 < len(nums2):
heapq.heappush(h, (nums1[i] + nums2[j+1], i, j + 1))
k -= 1
return res