Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
- int n = profits.size();
- vector<pair<int,int>> projects;
- // Create a vector of pairs of profits and capital for each project
- for(int i = 0; i < n; i++){
- projects.push_back({capital[i], profits[i]});
- }
- // Sort the projects based on their capital requirements
- sort(projects.begin(), projects.end());
- priority_queue<int> pq;
- int idx = 0;
- // Iterate k times to choose k distinct projects with the highest profits
- for(int i = 0; i < k; i++){
- while(idx < n && projects[idx].first <= w){
- pq.push(projects[idx].second);
- idx++;
- }
- if(!pq.empty()){
- w += pq.top();
- pq.pop();
- }
- else{
- break;
- }
- }
- return w;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment