Skip to content

Commit 2da4aa9

Browse files
committed
solved: 502. IPO
Signed-off-by: rajput-hemant <[email protected]>
1 parent b9dbaae commit 2da4aa9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/0501-0600/502 - IPO/Ipo.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Arrays;
2+
import java.util.Collections;
3+
import java.util.PriorityQueue;
4+
5+
class Pair implements Comparable<Pair> {
6+
int capital;
7+
int profit;
8+
9+
public Pair(int capital, int profit) {
10+
this.capital = capital;
11+
this.profit = profit;
12+
}
13+
14+
@Override
15+
public int compareTo(Pair other) {
16+
return this.capital - other.capital;
17+
}
18+
}
19+
20+
class Solution {
21+
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
22+
int n = profits.length;
23+
Pair[] pairs = new Pair[n];
24+
25+
for (int i = 0; i < n; i++) {
26+
pairs[i] = new Pair(capital[i], profits[i]);
27+
}
28+
29+
Arrays.sort(pairs);
30+
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
31+
int i = 0;
32+
33+
while (k > 0) {
34+
while (i < n && pairs[i].capital <= w) {
35+
pq.add(pairs[i].profit);
36+
i++;
37+
}
38+
if (pq.isEmpty()) {
39+
break;
40+
}
41+
w += pq.poll();
42+
k--;
43+
}
44+
45+
return w;
46+
}
47+
}

0 commit comments

Comments
 (0)