-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path502. IPO.cpp
42 lines (30 loc) · 850 Bytes
/
502. 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
37
38
39
40
41
42
/*
Problem : https://leetcode.com/problems/ipo/
Author : Sabbir Musfique
Time Complexity : O(n log(n))
*/
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
int n = profits.size();
vector<pair<int, int>> projects;
for (int i = 0; i < n; i++) {
projects.emplace_back(capital[i], profits[i]);
}
sort(projects.begin(), projects.end());
priority_queue<int> q;
int ptr = 0;
for (int i = 0; i < k; i++) {
while (ptr < n && projects[ptr].first <= w) {
q.push(projects[ptr++].second);
}
if (q.empty()) {
break;
}
w += q.top();
q.pop();
}
return w;
// sabbir 063
}
};