-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPO.cpp
36 lines (28 loc) · 793 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
33
34
35
36
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits,
vector<int>& capital) {
int n = profits.size();
// {capital, profit}
vector<pair<int, int>> proj(n);
for (int i = 0; i < n; i++) {
proj[i] = {capital[i], profits[i]};
}
sort(proj.begin(), proj.end());
priority_queue<int> pq;
int idx = 0;
while (k > 0) {
while (idx < n && proj[idx].first <= w) {
pq.push(proj[idx].second);
idx++;
}
if (pq.empty()) { // no available proj
return w;
}
w += pq.top();
pq.pop();
k--;
}
return w;
}
};