Sorting + One O(k)-size Priority Queue Solution
There are many excellent solutions based on two priority queue solution, however, we do not need to maintain two priority queues that contain all projects. \n I
There are many excellent solutions based on two priority queue solution, however, we do not need to maintain two priority queues that contain all projects. \n* If we sort the Capital in increasing order, we can insert "doable" project into the pq until we meet an "undoable" project. \n\n* We need only one priority queue (multiset) to maintain the "doable" projects. Here the key observation is: we can only pop k times. So we do not need to maintain a large priority queue. Every time we find the size of PQ is larger than k (k is shrinking!!!), we just erase the project with least profit from the PQ.\n\n* Note that the worst case time complexity is still O(NlogN), because we need to sort : )\n\n```\nclass Solution {\npublic:\n    struct Node {int profit, capital;};\n\n    int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {\n        if(Profits.empty() || Capital.empty()) return W;\n        vector<Node*> projects;\n        for(int i = 0; i < Profits.size(); i++) \n            projects.push_back(new Node({Profits[i], Capital[i]}));\n        multiset<int> pq;\n        sort(projects.begin(), projects.end(), [&](Node* n1, Node* n2) {return n1->capital < n2->capital;});\n        for(auto start = projects.begin(); k > 0; k--) {\n            for(; start != projects.end() && (*start)->capital <= W; start++) {\n                pq.insert((*start)->profit);\n                if(pq.size() > k) pq.erase(pq.begin());\n            } \n            if(pq.empty()) break;\n            W += *pq.rbegin();\n            pq.erase(prev(pq.end()));\n        }\n        return W;\n    }\n};\n```
[]