link
class Solution {
public:
struct Comp{
bool operator()(vector<int>& v1, vector<int>& v2){
return v1[0]+v1[1]>v2[0]+v2[1];
}
};
int kthSmallest(vector<vector<int>>& mat, int k) {
vector<int> sum=mat[0];
int m=mat.size();
for(int i=1;i<m;i++){
sum=helper(sum,mat[i]);
}
return sum[k-1];
}
vector<int> helper(vector<int>& num1, vector<int>& num2){
priority_queue<vector<int>, vector<vector<int>>, Comp> pq;
for(int i=0;i<num2.size();i++){
pq.push({num2[i],num1[0],0});
}
vector<int> res;
while(!pq.empty()){
auto cur=pq.top();
pq.pop();
res.push_back(cur[0]+cur[1]);
if(res.size()==200) break;
if(cur[2]<num1.size()-1){
pq.push({cur[0],num1[cur[2]+1],cur[2]+1});
}
}
return res;
}
};