-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0052-ipo.cpp
More file actions
33 lines (32 loc) · 947 Bytes
/
0052-ipo.cpp
File metadata and controls
33 lines (32 loc) · 947 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
27
28
29
30
31
32
33
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
priority_queue<pair<int, int>> pq;
int n = profits.size();
for (int i = 0;i < n;i++) {
pq.push({profits[i], capital[i]});
}
vector<pair<int, int>> rejected;
while(k&&!pq.empty()) {
auto top = pq.top();
pq.pop();
if (top.second <= w) {
w += top.first;
k--;
for (int j=0;j<rejected.size();j++) {
if (rejected[j].second <= w) {
pq.push(rejected[j]);
rejected.erase(rejected.begin()+j);
break;
}
}
} else {
rejected.push_back(top);
}
}
return w;
}
};