Fix Problem 373 (Find K Pairs with Smallest Sums) #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Report: Find K Pairs with Smallest Sums (LeetCode 373)
Problem Description
This problem asks us to find the
kpairs with the smallest sums from two given sorted integer arrays,nums1andnums2. A pair(u, v)consists of one elementufromnums1and one elementvfromnums2.Solution Approach: Min-Priority Queue (Min-Heap)
The problem is a classic "Top K" problem, best solved efficiently using a Min-Priority Queue (Min-Heap).
Reasoning:
Since both input arrays
nums1andnums2are sorted in non-decreasing order, the smallest possible sum will always come from(nums1[0], nums2[0]). From this starting point, we want to systematically explore subsequent pairs in an order that ensures we always pick the globally smallest available sum at each step. A min-heap naturally facilitates this by always providing the element with the lowest priority (in our case, the smallest sum).Algorithm Steps:
Initialize a Min-Heap: Create a min-priority queue. Each element in the heap will be a tuple
[sum, index1, index2], wheresumisnums1[index1] + nums2[index2]. The heap will be ordered bysum.Initial Population of Heap:
min(k, nums1.length)pairs formed bynums1[i]andnums2[0]to the min-heap.nums2[0]for initial pairs because for anynums1[i], pairing it withnums2[0]will yield the smallest sum involvingnums1[i]as the first element. Limiting tomin(k, nums1.length)prevents unnecessary additions ifnums1is very large butkis small.Extract K Smallest Pairs:
resultto store the output pairs.result.lengthis less thank:[currentSum, i, j].[nums1[i], nums2[j]]to theresultlist.j + 1is a valid index withinnums2(i.e.,j + 1 < nums2.length), it means we can potentially form a new smaller sum by pairingnums1[i]with the next element innums2. Push this new candidate[nums1[i] + nums2[j+1], i, j+1]onto the heap. This is crucial for correctly exploring the search space.Return
result: Oncekpairs are collected or the heap becomes empty,resultcontains thekpairs with the smallest sums.Complexity Analysis
Time Complexity: O(k log k)
min(k, nums1.length)elements to the heap takes approximatelyO(min(k, nums1.length) * log(min(k, nums1.length)))time. In the worst case, this isO(k log k).kelements from the heap. Eachpopoperation takesO(log H)time, whereHis the heap size. The maximum heap size isk. Eachpushoperation (for the next candidate) also takesO(log H)time. Since we performkpops and at mostkpushes, the total time for this phase isO(k log k).O(k log k).Space Complexity: O(k)
min_heapstores at mostkelements at any given time.resultlist storeskpairs.O(k).Code (JavaScript)