-
Notifications
You must be signed in to change notification settings - Fork 43
Fix Problem 373 (Find K Pairs with Smallest Sums) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Solution 373 has been updated to use the built-in PriorityQueue
class. It looks like support for MinPriorityQueue
was dropped. See: ed3e6f8
Thank you for your feedback.Could you also check and update the problem no.
295 and 407
... On 2025年7月17日, 8:40 am Josh Crozier, ***@***.***> wrote:
*JoshCrozier* left a comment (JoshCrozier/leetcode-javascript#1)
<#1 (comment)>
Solution 373 has been updated to use the built-in PriorityQueue class. It
looks like support for MinPriorityQueue was dropped. See: ed3e6f8
<ed3e6f8>
—
Reply to this email directly, view it on GitHub
<#1 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A6RBG3AQ75TPEXE7NFAKUIT3I4LLTAVCNFSM6AAAAACBUAJ4GOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTAOBSGM2DIMBUHA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
k
pairs with the smallest sums from two given sorted integer arrays,nums1
andnums2
. A pair(u, v)
consists of one elementu
fromnums1
and one elementv
fromnums2
.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
nums1
andnums2
are 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]
, wheresum
isnums1[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 ifnums1
is very large butk
is small.Extract K Smallest Pairs:
result
to store the output pairs.result.length
is less thank
:[currentSum, i, j]
.[nums1[i], nums2[j]]
to theresult
list.j + 1
is 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
: Oncek
pairs are collected or the heap becomes empty,result
contains thek
pairs 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)
.k
elements from the heap. Eachpop
operation takesO(log H)
time, whereH
is the heap size. The maximum heap size isk
. Eachpush
operation (for the next candidate) also takesO(log H)
time. Since we performk
pops and at mostk
pushes, the total time for this phase isO(k log k)
.O(k log k)
.Space Complexity: O(k)
min_heap
stores at mostk
elements at any given time.result
list storesk
pairs.O(k)
.Code (JavaScript)