373. Find K Pairs with Smallest Sums Medium

@problem@discussion
#Array#Heap (Priority Queue)



1/**
2 * [373] Find K Pairs with Smallest Sums
3 *
4 * You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
5 * Define a pair (u, v) which consists of one element from the first array and one element from the second array.
6 * Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.
7 *  
8 * Example 1:
9 * 
10 * Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
11 * Output: [[1,2],[1,4],[1,6]]
12 * Explanation: 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 * 
14 * Example 2:
15 * 
16 * Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
17 * Output: [[1,1],[1,1]]
18 * Explanation: 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]
19 * 
20 * Example 3:
21 * 
22 * Input: nums1 = [1,2], nums2 = [3], k = 3
23 * Output: [[1,3],[2,3]]
24 * Explanation: All possible pairs are returned from the sequence: [1,3],[2,3]
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	1 <= nums1.length, nums2.length <= 10^5
30 * 	-10^9 <= nums1[i], nums2[i] <= 10^9
31 * 	nums1 and nums2 both are sorted in ascending order.
32 * 	1 <= k <= 10^4
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/find-k-pairs-with-smallest-sums/
38// discuss: https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn k_smallest_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
44        vec![]
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_373() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.