Skip to content

Latest commit

 

History

History
executable file
·
24 lines (13 loc) · 1.11 KB

File metadata and controls

executable file
·
24 lines (13 loc) · 1.11 KB

373. Find K Pairs with Smallest Sums

ou 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.

Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.

1 <= nums1.length, nums2.length <= 104

-$10^9$ <= nums1[i], nums2[i] <= $10^9$

nums1 and nums2 both are sorted in ascending order.

1 <= k <= 1000

my firsts idea is use two pointer to point minimum one. And if we sum them the we will let bigger one move.

maybe we can use double two pointer to represent smaller one + smaller one corresponding number.

oh I find a pattern. if we have two list. the minimum one must come from the current minimum one + position or the next minimum one + his position.

Oh fuck. I am a idiot. 1 <= k <= 1000. Thus, we only need to get the first k number. because for example, the first k number is all minimum in the first array. Thus, we only need to calculate the k number.

Also, the std use lambda as compare function. That's so celever. We need to learn these language.