Skip to content

Commit 24ceed5

Browse files
refactor 502
1 parent f0762d4 commit 24ceed5

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/main/java/com/fishercoder/solutions/_502.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,27 @@
2929
*/
3030
public class _502 {
3131

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]});
4241
}
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];
4550
}
46-
W += profitHeap.poll()[1];
51+
return W;
4752
}
48-
return W;
4953
}
5054

5155
}

src/test/java/com/fishercoder/_502Test.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/2/17.
11-
*/
129
public class _502Test {
13-
private static _502 test;
10+
private static _502.Solution1 solution1;
1411
private static int[] Profits;
1512
private static int[] Capital;
1613

1714
@BeforeClass
1815
public static void setup() {
19-
test = new _502();
16+
solution1 = new _502.Solution1();
2017
}
2118

2219
@Test
2320
public void test1() {
2421
Profits = new int[]{1, 2, 3};
2522
Capital = new int[]{0, 1, 1};
26-
assertEquals(4, test.findMaximizedCapital(2, 0, Profits, Capital));
23+
assertEquals(4, solution1.findMaximizedCapital(2, 0, Profits, Capital));
2724
}
2825
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy