Skip to content

Commit f61d26b

Browse files
committed
Time: 192 ms (29.49%), Space: 9.3 MB (63.23%) - LeetHub
1 parent 9ef7131 commit f61d26b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
typedef pair<int,int> pii;
2+
class Solution {
3+
public:
4+
class cmp {
5+
public:
6+
bool operator()(const pii& p1, const pii& p2){
7+
return p1.first + p1.second < p2.first + p2.second;
8+
}
9+
};
10+
11+
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
12+
priority_queue<pii, vector<pii>, cmp> pq;// max-heap
13+
for(auto x: nums1){
14+
for(auto y: nums2){
15+
pq.push({x, y});
16+
if(pq.size() > k)
17+
pq.pop();
18+
}
19+
}
20+
21+
vector<vector<int>> ans;
22+
while(!pq.empty()){
23+
auto x = pq.top();
24+
pq.pop();
25+
26+
ans.push_back({x.first, x.second});
27+
}
28+
29+
sort(ans.begin(), ans.end());
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)