Concise and clean C++ solution inspired from G4G


#1
vector<int> Solution::solve(vector<int> &a, vector<int> &b, int c) {
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    priority_queue<tuple<int,int,int>> pq;
    pq.emplace(a[a.size()-1] + b[a.size()-1],a.size()-1,b.size()-1);
    set<pair<int,int>> s;
    s.emplace(a.size()-1,b.size()-1);
    vector<int> vec;
    while(c--)
    {
        auto[sum,i,j] = pq.top();
        pq.pop();
        vec.push_back(sum);
        if(i>0 && !s.count({i-1,j})) 
        {
            s.emplace(i-1,j);
            pq.emplace(a[i-1] + b[j],i-1,j);
        }
        if(j>0 && !s.count({i,j-1}))
        {
            s.emplace(i,j-1);
            pq.emplace(a[i] + b[j-1],i,j-1);
        }
    }
    return vec;
}