-
Notifications
You must be signed in to change notification settings - Fork 7
/
378.cpp
26 lines (23 loc) · 921 Bytes
/
378.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
class Solution {
public:
struct compare{
bool operator()(const pair<int,pair<int,int>> &p1,const pair<int,pair<int,int>> &p2){
return p1.first > p2.first;
}
};
int kthSmallest(vector<vector<int>>& matrix, int k) {
//template <class T, class Sequence = vector<T>, class Compare = less<typename Sequence::value_type> >
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,compare> pq;
for(int i = 0;i < matrix.size();i++)
pq.push(make_pair(matrix[i][0],make_pair(i,0)));
int res,cols = matrix[0].size();
while(k--){
res = pq.top().first;
int i = pq.top().second.first,j = pq.top().second.second;
pq.pop();
if(j < cols - 1)
pq.push(make_pair(matrix[i][j + 1],make_pair(i,j+1)));
}
return res;
}
};