Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
sx4im wants to merge 2 commits into JoshCrozier:master from sx4im:master

Conversation

Copy link

@sx4im sx4im commented Jul 16, 2025

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 and nums2. A pair (u, v) consists of one element u from nums1 and one element v from nums2.

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 and nums2 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:

  1. Initialize a Min-Heap: Create a min-priority queue. Each element in the heap will be a tuple [sum, index1, index2], where sum is nums1[index1] + nums2[index2]. The heap will be ordered by sum.

  2. Initial Population of Heap:

    • Add the first min(k, nums1.length) pairs formed by nums1[i] and nums2[0] to the min-heap.
    • We only need to consider nums2[0] for initial pairs because for any nums1[i], pairing it with nums2[0] will yield the smallest sum involving nums1[i] as the first element. Limiting to min(k, nums1.length) prevents unnecessary additions if nums1 is very large but k is small.
  3. Extract K Smallest Pairs:

    • Initialize an empty list result to store the output pairs.
    • While the heap is not empty and result.length is less than k:
      • Extract the smallest element from the heap: [currentSum, i, j].
      • Add the actual pair [nums1[i], nums2[j]] to the result list.
      • Explore Next Candidate: If j + 1 is a valid index within nums2 (i.e., j + 1 < nums2.length), it means we can potentially form a new smaller sum by pairing nums1[i] with the next element in nums2. Push this new candidate [nums1[i] + nums2[j+1], i, j+1] onto the heap. This is crucial for correctly exploring the search space.
  4. Return result: Once k pairs are collected or the heap becomes empty, result contains the k pairs with the smallest sums.

Complexity Analysis

  • Time Complexity: O(k log k)

    • Initialization: Adding min(k, nums1.length) elements to the heap takes approximately O(min(k, nums1.length) * log(min(k, nums1.length))) time. In the worst case, this is O(k log k).
    • Extraction Loop: We extract k elements from the heap. Each pop operation takes O(log H) time, where H is the heap size. The maximum heap size is k. Each push operation (for the next candidate) also takes O(log H) time. Since we perform k pops and at most k pushes, the total time for this phase is O(k log k).
    • Combining these, the dominant factor is O(k log k).
  • Space Complexity: O(k)

    • The min_heap stores at most k elements at any given time.
    • The result list stores k pairs.
    • Therefore, the space complexity is O(k).

Code (JavaScript)

class MinHeap {
 constructor() {
 this.heap = [];
 }
 size() {
 return this.heap.length;
 }
 push(element) {
 this.heap.push(element);
 this._bubbleUp(this.heap.length - 1);
 }
 pop() {
 if (this.heap.length === 0) {
 return null;
 }
 if (this.heap.length === 1) {
 return this.heap.pop();
 }
 const min = this.heap[0];
 this.heap[0] = this.heap.pop();
 this._bubbleDown(0);
 return min;
 }
 peek() {
 return this.heap.length > 0 ? this.heap[0] : null;
 }
 _bubbleUp(index) {
 while (index > 0) {
 const parentIndex = Math.floor((index - 1) / 2);
 if (this.heap[parentIndex][0] > this.heap[index][0]) {
 [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
 index = parentIndex;
 } else {
 break;
 }
 }
 }
 _bubbleDown(index) {
 const lastIndex = this.heap.length - 1;
 while (true) {
 let leftChildIndex = 2 * index + 1;
 let rightChildIndex = 2 * index + 2;
 let smallestChildIndex = index;
 if (leftChildIndex <= lastIndex && this.heap[leftChildIndex][0] < this.heap[smallestChildIndex][0]) {
 smallestChildIndex = leftChildIndex;
 }
 if (rightChildIndex <= lastIndex && this.heap[rightChildIndex][0] < this.heap[smallestChildIndex][0]) {
 smallestChildIndex = rightChildIndex;
 }
 if (smallestChildIndex !== index) {
 [this.heap[index], this.heap[smallestChildIndex]] = [this.heap[smallestChildIndex], this.heap[index]];
 index = smallestChildIndex;
 } else {
 break;
 }
 }
 }
}
var kSmallestPairs = function(nums1, nums2, k) {
 if (!nums1.length || !nums2.length || k === 0) {
 return [];
 }
 const minHeap = new MinHeap();
 const result = [];
 for (let i = 0; i < Math.min(k, nums1.length); i++) {
 minHeap.push([nums1[i] + nums2[0], i, 0]);
 }
 while (minHeap.size() > 0 && result.length < k) {
 const [currentSum, i, j] = minHeap.pop();
 result.push([nums1[i], nums2[j]]);
 if (j + 1 < nums2.length) {
 minHeap.push([nums1[i] + nums2[j + 1], i, j + 1]);
 }
 }
 return result;
};

Copy link
Owner

Solution 373 has been updated to use the built-in PriorityQueue class. It looks like support for MinPriorityQueue was dropped. See: ed3e6f8

Copy link
Author

sx4im commented Jul 17, 2025 via email

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
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /