-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path502.cpp
More file actions
29 lines (26 loc) · 913 Bytes
/
502.cpp
File metadata and controls
29 lines (26 loc) · 913 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
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
priority_queue<int> pq;
vector<pair<int, int>> projects;
int n = profits.size();
for (int i = 0; i < n; ++i) projects.push_back(make_pair(capital[i], profits[i]));
sort(projects.begin(), projects.end());
int projectIdx = 0;
while (projectIdx < n && projects[projectIdx].first <= w) {
pq.push(projects[projectIdx].second);
projectIdx++;
}
while (!pq.empty() && k) {
int currMax = pq.top();
pq.pop();
w += currMax;
while (projectIdx < n && projects[projectIdx].first <= w) {
pq.push(projects[projectIdx].second);
projectIdx++;
}
k--;
}
return w;
}
};