Find K Pairs with Smallest Sums
Problem Statement
Section titled “Problem Statement”LeetCode #373 - Find K Pairs with Smallest Sums
Section titled “LeetCode #373 - Find K Pairs with Smallest Sums”Solve this classic coding interview problem efficiently using multiple approaches.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
[example1] | result1 | Explanation goes here |
[example2] | result2 | Explanation goes here |
Constraints
Section titled “Constraints”- Standard LeetCode constraints
- Refer to the official problem statement for exact details
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Approach 1 | O(n) | O(1) or O(n) | General case |
| Approach 2 | O(n log n) | O(n) | Alternative |
| Approach 3 | O(n²) | O(1) | Educational |
Which approach should you learn first?
Section titled “Which approach should you learn first?”- Pick Approach 1 if you want the optimal solution.
- Pick Approach 3 if you are learning the fundamentals.
- Pick Approach 2 for interview preparation.
Recommended
Approach 1
O(n) time · Minimal space
Alternative
Approach 2
O(n log n) time · More space
Learning
Approach 3
O(n²) time · No extra space
Approach 1: Min Heap (Recommended)
Section titled “Approach 1: Min Heap (Recommended)”Use a min heap to efficiently track the k smallest pairs. Start with the pair (nums1[0], nums2[0]) and explore neighboring pairs in increasing order.
⏱ Time O(k log n) k iterations 💾 Space O(n) Heap storage
Solution Code
Section titled “Solution Code”from typing import List
def find_k_pairs_with_smallest_sum_min_heap(nums: List[int]) -> int: """ Find K Pairs With Smallest Sum - Min Heap approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_k_pairs_with_smallest_sum_min_heap(vector<int>& nums) { // Find K Pairs With Smallest Sum - Min Heap approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_k_pairs_with_smallest_sum_min_heap(int[] nums) { // Find K Pairs With Smallest Sum - Min Heap approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_k_pairs_with_smallest_sumMinheap = function(nums) { // Find K Pairs With Smallest Sum - Min Heap approach return 0;};
if (require.main === module) { // Test cases}pub fn find_k_pairs_with_smallest_sum_min_heap(nums: Vec<i32>) -> i32 { // Find K Pairs With Smallest Sum - Min Heap approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_k_pairs_with_smallest_sumMinheap(nums []int) int { // Find K Pairs With Smallest Sum - Min Heap approach return 0}
func main() { // Test cases}Approach 2: Two-Pointer
Section titled “Approach 2: Two-Pointer”Place one pointer at the end of the first array and one at the beginning of the second array. Adjust pointers based on the pair sum.
⏱ Time O(n log n) Pointer movement 💾 Space O(1) No extra space
Solution Code
Section titled “Solution Code”from typing import List
def find_k_pairs_with_smallest_sum_two_pointer(nums: List[int]) -> int: """ Find K Pairs With Smallest Sum - Two Pointer approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_k_pairs_with_smallest_sum_two_pointer(vector<int>& nums) { // Find K Pairs With Smallest Sum - Two Pointer approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_k_pairs_with_smallest_sum_two_pointer(int[] nums) { // Find K Pairs With Smallest Sum - Two Pointer approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_k_pairs_with_smallest_sumTwopointer = function(nums) { // Find K Pairs With Smallest Sum - Two Pointer approach return 0;};
if (require.main === module) { // Test cases}pub fn find_k_pairs_with_smallest_sum_two_pointer(nums: Vec<i32>) -> i32 { // Find K Pairs With Smallest Sum - Two Pointer approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_k_pairs_with_smallest_sumTwopointer(nums []int) int { // Find K Pairs With Smallest Sum - Two Pointer approach return 0}
func main() { // Test cases}Approach 3: Brute Force
Section titled “Approach 3: Brute Force”Generate all possible pairs and sort them by sum. Return the first k pairs.
⏱ Time O(n² log n) All pairs 💾 Space O(n²) All pairs stored
Solution Code
Section titled “Solution Code”from typing import List
def find_k_pairs_with_smallest_sum_brute_force(nums: List[int]) -> int: """ Find K Pairs With Smallest Sum - Brute Force approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_k_pairs_with_smallest_sum_brute_force(vector<int>& nums) { // Find K Pairs With Smallest Sum - Brute Force approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_k_pairs_with_smallest_sum_brute_force(int[] nums) { // Find K Pairs With Smallest Sum - Brute Force approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_k_pairs_with_smallest_sumBruteforce = function(nums) { // Find K Pairs With Smallest Sum - Brute Force approach return 0;};
if (require.main === module) { // Test cases}pub fn find_k_pairs_with_smallest_sum_brute_force(nums: Vec<i32>) -> i32 { // Find K Pairs With Smallest Sum - Brute Force approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_k_pairs_with_smallest_sumBruteforce(nums []int) int { // Find K Pairs With Smallest Sum - Brute Force approach return 0}
func main() { // Test cases}