Find K Pairs with Smallest Sums

Given two sorted arrays, return the k pairs with the smallest sums. To make the output deterministic, sort pairs by sum ascending, then by the first value ascending, then by the second value ascending.

Input Format

nums1 and nums2 are sorted arrays, k is number of pairs to return

Output Format

k pairs with the smallest sums in deterministic order

Constraints

  • 1 <= nums1.length, nums2.length <= 10^5
  • 1 <= k <= 10^4
  • -10^9 <= values <= 10^9

Examples

Example 1:

Input:

nums1 = [1,7,11]
nums2 = [2,4,6]
k = 3

Output:

[[1,2],[1,4],[1,6]]

Explanation:

The three smallest sums all use 1 from nums1.

Example 2:

Input:

nums1 = [1,1,2]
nums2 = [1,2,3]
k = 2

Output:

[[1,1],[1,1]]

Explanation:

The smallest sum pair appears twice because nums1 contains duplicate 1s.

Loading...
Find K Pairs with Smallest Sums - Heap