|
29 | 29 | */
|
30 | 30 | public class _502 {
|
31 | 31 |
|
32 |
| - /**credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues*/ |
33 |
| - public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) { |
34 |
| - PriorityQueue<int[]> capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
35 |
| - PriorityQueue<int[]> profitHeap = new PriorityQueue<>((a,b) -> b[1] - a[1]); |
36 |
| - for (int i = 0; i < Profits.length; i++) { |
37 |
| - capitalHeap.add(new int[]{Capital[i], Profits[i]}); |
38 |
| - } |
39 |
| - while (k-- > 0) { |
40 |
| - while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) { |
41 |
| - profitHeap.add(capitalHeap.poll()); |
| 32 | + public static class Solution1 { |
| 33 | + /** |
| 34 | + * credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues |
| 35 | + */ |
| 36 | + public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) { |
| 37 | + PriorityQueue<int[]> capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 38 | + PriorityQueue<int[]> profitHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]); |
| 39 | + for (int i = 0; i < Profits.length; i++) { |
| 40 | + capitalHeap.add(new int[]{Capital[i], Profits[i]}); |
42 | 41 | }
|
43 |
| - if (profitHeap.isEmpty()) { |
44 |
| - break; |
| 42 | + while (k-- > 0) { |
| 43 | + while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) { |
| 44 | + profitHeap.add(capitalHeap.poll()); |
| 45 | + } |
| 46 | + if (profitHeap.isEmpty()) { |
| 47 | + break; |
| 48 | + } |
| 49 | + W += profitHeap.poll()[1]; |
45 | 50 | }
|
46 |
| - W += profitHeap.poll()[1]; |
| 51 | + return W; |
47 | 52 | }
|
48 |
| - return W; |
49 | 53 | }
|
50 | 54 |
|
51 | 55 | }
|
0 commit comments