Find K Pairs with Smallest Sums - Complete Solution Guide
Find K Pairs with Smallest Sums is LeetCode problem 373, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given two integer arrays nums1 and nums2 sorted in non-decreasing 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 (u 1 , v 1 ), (u 2 , v 2 ), ..., (u k , v k ) with the smallest sums . Example 1: Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3 Output: [[1,2],[1,4],[1,6]] 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],
Detailed Explanation
The problem asks us to find the 'k' pairs with the smallest sums from two sorted integer arrays, `nums1` and `nums2`. A pair consists of one element from `nums1` and one element from `nums2`. The output should be a list of these 'k' pairs, ordered by their sums in ascending order. The constraints specify that both arrays are sorted in non-decreasing order, and 'k' is within reasonable bounds. We are guaranteed that k is less than or equal to the product of the lengths of the input arrays.
Solution Approach
The provided solution uses a min-heap to efficiently find the k smallest pairs. It leverages the sorted nature of the input arrays. It maintains a min-heap of (sum, index_in_nums1, index_in_nums2) tuples. Initially, it adds pairs formed by elements from nums1 (up to k elements) and the first element of nums2 into the heap. Then, it iteratively extracts the pair with the smallest sum from the heap, adds it to the result, and adds the next possible pair (same element from nums1, next element from nums2) back into the heap if the next element exists in nums2. The loop continues until k pairs are found or the heap is empty.
Step-by-Step Algorithm
- Step 1: Initialize an empty min-heap (priority queue) to store tuples of (sum, index_in_nums1, index_in_nums2).
- Step 2: Initialize an empty result list to store the k smallest pairs.
- Step 3: Iterate through the first 'k' elements (or all elements if nums1.length < k) of nums1. For each element, create a pair with the first element of nums2 and push (sum, index_in_nums1, 0) onto the min-heap.
- Step 4: While k > 0 and the min-heap is not empty, do the following:
- Step 5: Pop the element with the smallest sum from the min-heap. This gives you (sum, i, j), where 'i' is the index in nums1 and 'j' is the index in nums2.
- Step 6: Create a pair (nums1[i], nums2[j]) and add it to the result list.
- Step 7: Decrement k.
- Step 8: If j + 1 is within the bounds of nums2, create a new pair using nums1[i] and nums2[j+1], and push (sum = nums1[i] + nums2[j+1], i, j+1) onto the min-heap.
- Step 9: Return the result list.
Key Insights
- Insight 1: Using a min-heap (priority queue) is crucial for efficiently tracking the smallest sums. It allows us to retrieve the smallest sum at any given moment.
- Insight 2: The sorted nature of the input arrays allows for an optimization. We don't need to compute all possible pairs upfront. We can initially consider pairs formed by the first element of `nums2` with the first few elements of `nums1` (up to 'k' elements).
- Insight 3: Instead of re-sorting elements after adding a new element, the heap property can be maintained by pushing an element only when a smaller sum has been achieved.
Complexity Analysis
Time Complexity: O(k log k)
Space Complexity: O(k)
Topics
This problem involves: Array, Heap (Priority Queue).
Companies
Asked at: LinkedIn.