-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15jun.txt
40 lines (33 loc) · 1.19 KB
/
15jun.txt
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
37
38
39
40
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits,
vector<int>& capital) {
int n = profits.size();
std::vector<std::pair<int, int>> projects;
// Creating vector of pairs (capital, profits)
for (int i = 0; i < n; ++i) {
projects.emplace_back(capital[i], profits[i]);
}
// Sorting projects by capital required
std::sort(projects.begin(), projects.end());
// Max-heap to store profits, using greater to create a max-heap
std::priority_queue<int> maxHeap;
int i = 0;
// Main loop to select up to k projects
for (int j = 0; j < k; ++j) {
// Add all profitable projects that we can afford
while (i < n && projects[i].first <= w) {
maxHeap.push(projects[i].second);
i++;
}
// If no projects can be funded, break out of the loop
if (maxHeap.empty()) {
break;
}
// Otherwise, take the project with the maximum profit
w += maxHeap.top();
maxHeap.pop();
}
return w;
}
};