-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy path373. Find K Pairs with Smallest Sums.cpp
30 lines (29 loc) · 1.2 KB
/
373. 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
// Solution 1. MinHeap, time: O(m * n * log(m*n))
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
auto comp = [](pair<int, int>& p1, pair<int, int>& p2){ return p1.first + p1.second > p2.first + p2.second; };
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)>pq(comp);
for(auto x: nums1)
for(auto y: nums2) pq.push({x, y});
vector<pair<int, int>>res;
while(k-- && !pq.empty()) res.push_back(pq.top()), pq.pop();
return res;
}
};
// Solution 2. MaxHeap, time: O(m * n * log(k)).
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
auto comp = [](pair<int, int>& p1, pair<int, int>& p2){ return p1.first + p1.second < p2.first + p2.second; };
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)>pq(comp);
for(auto x: nums1)
for(auto y: nums2){
pq.push({x, y});
if(pq.size() > k) pq.pop();
}
vector<pair<int, int>>res;
while(!pq.empty()) res.push_back(pq.top()), pq.pop();
return res;
}
};