Skip to content

Commit 0e7e2ff

Browse files
committed
373
1 parent 95445ac commit 0e7e2ff

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Leetcode solutions in C++, Java, Python
33

44
|#|Title|Solution|Difficulty|
55
|---|-----|--------|----------|
6+
|373|[Find K Pairs with Smallest Sums](./src/find_k_pairs_with_smallest_sums)|[C++](./src/find_k_pairs_with_smallest_sums)|Medium|
67
|372|[Super Pow](./src/super_pow)|[C++](./src/super_pow)|Medium|
78
|371|[Sum of Two Integers](./src/sum_of_two_integers)|[C++](./src/sum_of_two_integers)|Easy|
89
|370|[Range Addition](./src/range_addition)|[C++](./src/range_addition)|Medium|

Diff for: src/find_k_pairs_with_smallest_sums/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all:
2+
clang++ main.cpp -o main.o -std=c++11
3+
4+
run:
5+
clang++ main.cpp -o main.o -std=c++11
6+
./main.o

Diff for: src/find_k_pairs_with_smallest_sums/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Problem
2+
===
3+
4+
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
5+
6+
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
7+
8+
Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
9+
10+
11+
Solution
12+
===
13+
14+
Find all possible pairs and sort
15+
16+
O(k^2 logk)

Diff for: src/find_k_pairs_with_smallest_sums/main.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <algorithm>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <map>
5+
#include <queue>
6+
#include <set>
7+
#include <sstream>
8+
#include <stack>
9+
#include <string>
10+
#include <unordered_map>
11+
#include <unordered_set>
12+
#include <vector>
13+
14+
using namespace std;
15+
16+
bool comp_pairs (pair<int, int> p, pair<int, int> q) {
17+
return (p.first + p.second < q.first + q.second);
18+
}
19+
20+
class Solution {
21+
public:
22+
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
23+
vector<pair<int, int>> result;
24+
for (int i = 0; i < k && i < nums1.size(); i++) {
25+
for (int j = 0; j < k - i && j < nums2.size(); j++) {
26+
result.push_back(make_pair(nums1[i], nums2[j]));
27+
}
28+
}
29+
sort(result.begin(), result.end(), comp_pairs);
30+
if (result.size() < k) {
31+
return result;
32+
}
33+
vector<pair<int, int>> ret(result.begin(), result.begin() + k);
34+
return ret;
35+
}
36+
};
37+
38+
int main() {
39+
Solution sol;
40+
vector<int> a, b;
41+
int k;
42+
43+
a = {1, 7, 11};
44+
b = {2, 4, 6};
45+
k = 3;
46+
for (auto p: sol.kSmallestPairs(a, b, k)) {
47+
cout << p.first << ", " << p.second << endl;
48+
}
49+
cout << endl;
50+
51+
a = {1, 1, 2};
52+
b = {1, 2, 3};
53+
k = 2;
54+
for (auto p: sol.kSmallestPairs(a, b, k)) {
55+
cout << p.first << ", " << p.second << endl;
56+
}
57+
cout << endl;
58+
59+
a = {1, 2};
60+
b = {3};
61+
k = 2;
62+
for (auto p: sol.kSmallestPairs(a, b, k)) {
63+
cout << p.first << ", " << p.second << endl;
64+
}
65+
cout << endl;
66+
67+
a = {};
68+
b = {};
69+
k = 5;
70+
for (auto p: sol.kSmallestPairs(a, b, k)) {
71+
cout << p.first << ", " << p.second << endl;
72+
}
73+
cout << endl;
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)