-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiPO.cpp
More file actions
28 lines (27 loc) · 853 Bytes
/
iPO.cpp
File metadata and controls
28 lines (27 loc) · 853 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
// Source: https://leetcode.com/problems/ipo/
// Author: Miao Zhang
// Date: 2021-02-19
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<int> heap;
multiset<pair<int, int>> capital; // {capital, profits}
for (int i = 0; i < Profits.size(); i++) {
if (Profits[i] <= 0) continue;
if (Capital[i] <= W) {
heap.push(Profits[i]);
} else {
capital.emplace(Capital[i], Profits[i]);
}
}
auto it = capital.cbegin();
while (!heap.empty() && k--) {
W += heap.top();
heap.pop();
while (it != capital.end() && it->first <= W) {
heap.push(it++->second);
}
}
return W;
}
};