File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int findMaximizedCapital (int k , int w , int [] profits , int [] capital ) {
3+ // Max-heap for profits of affordable projects
4+ Queue <Integer > maxProfit = new PriorityQueue <>(Comparator .reverseOrder ());
5+
6+ // Min-heap for (capital, profit) pairs
7+ Queue <int []> minCapital = new PriorityQueue <>(Comparator .comparingInt (a -> a [0 ]));
8+
9+ for (int i = 0 ; i < capital .length ; i ++) {
10+ minCapital .add (new int [] { capital [i ], profits [i ] });
11+ }
12+
13+ for (int i = 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+ return w ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments