-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path373. Find K Pairs with Smallest Sums
34 lines (31 loc) · 1.3 KB
/
373. Find K Pairs with Smallest Sums
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
31
32
33
34
class Solution {
public:
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<vector<int>> ans;
priority_queue<pair<int, pair<int, int>>> pq; // max-heap to store the k smallest pairs
for (int i = 0; i < nums1.size(); i++) {
for (int j = 0; j < nums2.size(); j++) {
int sum = nums1[i] + nums2[j];
// If the priority queue size is less than k, add the pair
if (pq.size() < k)
pq.push({sum, {nums1[i], nums2[j]}});
else if (sum < pq.top().first) {
// If the current sum is smaller than the largest sum in the priority queue,
// remove the largest sum and add the current sum
pq.pop();
pq.push({sum, {nums1[i], nums2[j]}});
} else if (sum > pq.top().first) {
// If the current sum is larger than the largest sum in the priority queue,
// break the inner loop since sums will only increase from this point onwards
break;
}
}
}
// Retrieve the k smallest pairs from the priority queue and store them in the answer vector
while (!pq.empty()) {
ans.push_back({pq.top().second.first, pq.top().second.second});
pq.pop();
}
return ans;
}
};