[C++][K Largest Elements] maintain K elements in min heap


#1
vector<int> Solution::solve(vector<int> &A, int B) {
    priority_queue<int, vector<int>, greater<int> > pq;
    vector<int> vec;
    for(auto it: A) {
        if(pq.size() == B and pq.top() < it)    pq.pop();
        if(pq.size() < B)        pq.push(it);
    }
    for(int i=0; i<B; i++) {
        vec.push_back(pq.top());
        pq.pop();
    }
    return vec;
}