Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
691 B
1
Indexable
Never
class Solution {
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& cap) {
        priority_queue<int> pq;
        vector<pair<int, int>> projects;
        for (int i = 0; i < cap.size(); ++i) {
            projects.push_back({cap[i], profits[i]});
        }
        sort(projects.begin(), projects.end());
        int i = 0;
        while (k--) {
            while (i < projects.size() && projects[i].first <= w) {
                pq.push(projects[i].second);
                ++i;
            }
            if (!pq.empty()) {
                w += pq.top();
                pq.pop();
            }
        }
        return w;
    }
};
Leave a Comment