-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathIPO.cpp
32 lines (32 loc) · 973 Bytes
/
IPO.cpp
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
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pqsg;
priority_queue<pair<int, int>> pqgs;
int n = capital.size();
for(int i = 0; i < n; i++)
{
//int val = profit[i]-capital[i];
if(capital[i] <= w)
{
pqgs.push({profits[i],capital[i]});
}
else if(capital[i] > w)
{
pqsg.push({capital[i],profits[i]});
}
}
while(k-- && !pqgs.empty())
{
pair<int, int> tmp = pqgs.top();
w += tmp.first;
pqgs.pop();
while(!pqsg.empty() && pqsg.top().first <= w)
{
pqgs.push({pqsg.top().second,pqsg.top().first});
pqsg.pop();
}
}
return w;
}
};