You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.
Example 1:
Example 2:
Example 3:
解法1:
discussion中的解法。要用到sorted这个条件可以简化不少程序。
由于两个都是sorted,那么第一个array中任何一个数字和第二个数组搭配时,最小的和一定是和第二个数组的第一个元素的配搭。
这样我们可以先把这个组合加入heap中,每次poll出来的时候就把对应的第二个数组的配搭往上挪一个。
|
|