Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2180e31

Browse files
Create 0502-ipo.java
1 parent32bb838 commit2180e31

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎java/0502-ipo.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classSolution {
2+
publicintfindMaximizedCapital(intk,intw,int[]profits,int[]capital) {
3+
// Max-heap for profits of affordable projects
4+
Queue<Integer>maxProfit =newPriorityQueue<>(Comparator.reverseOrder());
5+
6+
// Min-heap for (capital, profit) pairs
7+
Queue<int[]>minCapital =newPriorityQueue<>(Comparator.comparingInt(a ->a[0]));
8+
9+
for (inti =0;i <capital.length;i++) {
10+
minCapital.add(newint[] {capital[i],profits[i] });
11+
}
12+
13+
for (inti =0;i <k;i++) {
14+
// Add all affordable projects to the maxProfit heap
15+
while (!minCapital.isEmpty() &&minCapital.peek()[0] <=w) {
16+
int[]project =minCapital.poll();
17+
maxProfit.add(project[1]);
18+
}
19+
20+
// If there are no affordable projects, break
21+
if (maxProfit.isEmpty()) {
22+
break;
23+
}
24+
25+
// Select the project with the maximum profit
26+
w +=maxProfit.poll();
27+
}
28+
29+
returnw;
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp