g0501_0600.s0502_ipo.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0502_ipo;
// #Hard #Array #Sorting #Greedy #Heap_Priority_Queue
import java.util.Comparator;
import java.util.PriorityQueue;
public class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
PriorityQueue minCapital =
new PriorityQueue<>(Comparator.comparingInt((int[] a) -> a[1]));
PriorityQueue 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;
}
}