forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (29 loc) · 1.14 KB
/
Solution.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
32
package g0501_0600.s0502_ipo;
// #Hard #Array #Sorting #Greedy #Heap_Priority_Queue #Top_Interview_150_Heap
// #2025_03_09_Time_64_ms_(97.22%)_Space_59.96_MB_(87.77%)
import java.util.Comparator;
import java.util.PriorityQueue;
public class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
PriorityQueue<int[]> minCapital =
new PriorityQueue<>(Comparator.comparingInt((int[] a) -> a[1]));
PriorityQueue<int[]> maxProfit = new PriorityQueue<>((int[] a, int[] b) -> b[0] - a[0]);
for (int i = 0; i < profits.length; i++) {
if (w >= capital[i]) {
maxProfit.offer(new int[] {profits[i], capital[i]});
} else {
minCapital.offer(new int[] {profits[i], capital[i]});
}
}
int count = 0;
while (count < k && !maxProfit.isEmpty()) {
int[] temp = maxProfit.poll();
w += temp[0];
count += 1;
while (!minCapital.isEmpty() && minCapital.peek()[1] <= w) {
maxProfit.offer(minCapital.poll());
}
}
return w;
}
}