forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_502.java
30 lines (26 loc) · 1.02 KB
/
_502.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
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _502 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues
*/
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
PriorityQueue<int[]> capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
PriorityQueue<int[]> profitHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]);
for (int i = 0; i < Profits.length; i++) {
capitalHeap.add(new int[]{Capital[i], Profits[i]});
}
while (k-- > 0) {
while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) {
profitHeap.add(capitalHeap.poll());
}
if (profitHeap.isEmpty()) {
break;
}
W += profitHeap.poll()[1];
}
return W;
}
}
}