Skip to content

Commit 2180e31

Browse files
Create 0502-ipo.java
1 parent 32bb838 commit 2180e31

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

java/0502-ipo.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
3+
// Max-heap for profits of affordable projects
4+
Queue<Integer> maxProfit = new PriorityQueue<>(Comparator.reverseOrder());
5+
6+
// Min-heap for (capital, profit) pairs
7+
Queue<int[]> minCapital = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
8+
9+
for (int i = 0; i < capital.length; i++) {
10+
minCapital.add(new int[] { capital[i], profits[i] });
11+
}
12+
13+
for (int i = 0; i < k; i++) {
14+
// Add all affordable projects to the maxProfit heap
15+
while (!minCapital.isEmpty() && minCapital.peek()[0] <= w) {
16+
int[] project = minCapital.poll();
17+
maxProfit.add(project[1]);
18+
}
19+
20+
// If there are no affordable projects, break
21+
if (maxProfit.isEmpty()) {
22+
break;
23+
}
24+
25+
// Select the project with the maximum profit
26+
w += maxProfit.poll();
27+
}
28+
29+
return w;
30+
}
31+
}

0 commit comments

Comments
 (0)