← 返回 apple 的题目列表K Smallest Pairs (with Test Cases)
类型:online_judge
Given two integer arrays nums1 and nums2 sorted in non-decreasing order, and an integer k, return the k pairs (u, v) (where u is from nums1 and v is from nums2) with the smallest sums u+v.
Requirements:
Implement runnable code and write/add test cases to validate the output.
The returned pairs should be ordered by increasing sum. If sums are tied, any order is acceptable.
Input/Output
Input: nums1, nums2, k
Output: a list of the k smallest-sum pairs
Constraints
0 <= len(nums1), len(nums2) <= 1e5
-1e9 <= nums1[i], nums2[j] <= 1e9
0 <= k <= 1e4
nums1 and nums2 are already sorted (non-decreasing)
Sample Tests
nums1=[1,7,11], nums2=[2,4,6], k=3 → [[1,2],[1,4],[1,6]]
nums1=[1,1,2], nums2=[1,2,3], k=2 → [[1,1],[1,1]]
nums1=[1,2], nums2=[3], k=3 → [[1,3],[2,3]]
nums1=[], nums2=[1,2], k=5 → []
nums1=[-5,-2,0], nums2=[-3,1,4], k=4 → [[-5,-3],[-2,-3],[-5,1],[0,-3]] (any order for ties)
Example
Input
nums1=[1,7,11]
nums2=[2,4,6]
k=3
Output
[[1,2],[1,4],[1,6]]