-
Notifications
You must be signed in to change notification settings - Fork 28
/
Find_K_Pairs_with_Smallest_Sums.cpp
30 lines (28 loc) · 1.08 KB
/
Find_K_Pairs_with_Smallest_Sums.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
priority_queue <pair<long long, pair<int, int>>> maxHeap;
for(int i = 0; i < (int)nums1.size(); ++i) {
for(int j = 0; j < (int)nums2.size(); ++j) {
if((int)maxHeap.size() == k and nums1[i] + nums2[j] >= maxHeap.top().first) {
break;
}
if((int)maxHeap.size() < k) {
maxHeap.push({nums1[i] + nums2[j], {nums1[i], nums2[j]}});
} else {
if(maxHeap.top().first > nums1[i] + nums2[j]) {
maxHeap.pop();
maxHeap.push({nums1[i] + nums2[j], {nums1[i], nums2[j]}});
}
}
}
}
vector<pair<int, int>> result;
result.resize((int)maxHeap.size());
while(!maxHeap.empty()) {
result[(int)maxHeap.size() - 1] = maxHeap.top().second;
maxHeap.pop();
}
return result;
}
};