-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0502-ipo.java
31 lines (25 loc) · 1.09 KB
/
0502-ipo.java
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
class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
// Max-heap for profits of affordable projects
Queue<Integer> maxProfit = new PriorityQueue<>(Comparator.reverseOrder());
// Min-heap for (capital, profit) pairs
Queue<int[]> minCapital = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
for (int i = 0; i < capital.length; i++) {
minCapital.add(new int[] { capital[i], profits[i] });
}
for (int i = 0; i < k; i++) {
// Add all affordable projects to the maxProfit heap
while (!minCapital.isEmpty() && minCapital.peek()[0] <= w) {
int[] project = minCapital.poll();
maxProfit.add(project[1]);
}
// If there are no affordable projects, break
if (maxProfit.isEmpty()) {
break;
}
// Select the project with the maximum profit
w += maxProfit.poll();
}
return w;
}
}