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:
1 2 3 4 5 6 |
|
Example 2:
1 2 3 4 5 6 |
|
Example 3:
1 2 3 4 5 6 |
|
Solution
- 运用min-heap 关键是不知道nums1[i+1]+nums2[0] 与 nums1[i]+nums2[j] 之间的关系
- Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
- Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|