-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54_IPO.cpp
More file actions
26 lines (24 loc) · 727 Bytes
/
54_IPO.cpp
File metadata and controls
26 lines (24 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
vector <pair <int,int>> arr;
for(int i = 0; i<profits.size(); i++) arr.push_back({capital[i],profits[i]});
sort(arr.begin(),arr.end());
priority_queue <int> q;
int ans = 0, i=0;
while(i<arr.size() and k){
if(w>=arr[i].first) q.push(arr[i++].second);
else{
if(q.empty()) return w;
w += q.top();
q.pop();
k--;
}
}
while(k-- and !q.empty()){
w += q.top();
q.pop();
}
return w;
}
};