-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0502.IPO.cpp
More file actions
36 lines (31 loc) · 985 Bytes
/
0502.IPO.cpp
File metadata and controls
36 lines (31 loc) · 985 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
34
35
36
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Create sorted list (by capital) of projects.
const int n = min(profits.size(), capital.size());
vector<pair<int, int>> projects;
projects.reserve(n);
for (int i = 0; i < n; i++) projects.emplace_back(capital[i], profits[i]);
sort(projects.begin(), projects.end());
// Work projects.
int workingIndex = 0;
priority_queue<int> availableProfit;
while (k > 0) {
// Decrement k.
k--;
// Add available projects to list.
while (workingIndex < n && projects[workingIndex].first <= w)
availableProfit.push(projects[workingIndex++].second);
// Make sure we've got something to do.
if (availableProfit.empty()) break;
// Take project with maximum profits.
w += availableProfit.top();
availableProfit.pop();
}
return w;
}
};