Medium
Quiz
#373 Find K Pairs with Smallest Sums
APPROACH
Two arrays nums1 and nums2 are sorted in non-decreasing order. A pair (u, v) picks one element u from nums1 and one element v from nums2.
Find the k pairs whose element sums are the smallest and return them.
Example 1:
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
Output: [[1,2],[1,4],[1,6]]
Explanation: The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:
Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
Output: [[1,1],[1,1]]
Explanation: The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
1 of 4
1:00
What is the optimal approach for this problem?