|
| 1 | +<h2>373. Find K Pairs with Smallest Sums</h2><h3>Medium</h3><hr><div><p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>ascending order</strong> and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> |
| 4 | + |
| 5 | +<p>Return <em>the</em> <code>k</code> <em>pairs</em> <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> <em>with the smallest sums</em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> nums1 = [1,7,11], nums2 = [2,4,6], k = 3 |
| 11 | +<strong>Output:</strong> [[1,2],[1,4],[1,6]] |
| 12 | +<strong>Explanation:</strong> The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong>Example 2:</strong></p> |
| 16 | + |
| 17 | +<pre><strong>Input:</strong> nums1 = [1,1,2], nums2 = [1,2,3], k = 2 |
| 18 | +<strong>Output:</strong> [[1,1],[1,1]] |
| 19 | +<strong>Explanation:</strong> The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong>Example 3:</strong></p> |
| 23 | + |
| 24 | +<pre><strong>Input:</strong> nums1 = [1,2], nums2 = [3], k = 3 |
| 25 | +<strong>Output:</strong> [[1,3],[2,3]] |
| 26 | +<strong>Explanation:</strong> All possible pairs are returned from the sequence: [1,3],[2,3] |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Constraints:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li><code>1 <= nums1.length, nums2.length <= 10<sup>4</sup></code></li> |
| 34 | + <li><code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li> |
| 35 | + <li><code>nums1</code> and <code>nums2</code> both are sorted in <strong>ascending order</strong>.</li> |
| 36 | + <li><code>1 <= k <= 1000</code></li> |
| 37 | +</ul> |
| 38 | +</div> |
0 commit comments