-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_502.java
65 lines (47 loc) · 2.4 KB
/
Leetcode_502.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.
You are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.
Initially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.
Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.
The answer is guaranteed to fit in a 32-bit signed integer.
==============================================================================
*/
class Solution {
// Defining the Project class within the Solution class
private static class Project {
int capital;
int profit;
Project(int capital, int profit) {
this.capital = capital;
this.profit = profit;
}
}
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
List<Project> projects = new ArrayList<>();
// Creating list of projects with capital and profits
for (int i = 0; i < n; i++) {
projects.add(new Project(capital[i], profits[i]));
}
// Sorting projects by capital required
Collections.sort(projects, (a, b) -> a.capital - b.capital);
// Max-heap to store profits (using a min-heap with inverted values)
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((x, y) -> y - x);
int i = 0;
// Main loop to select up to k projects
for (int j = 0; j < k; j++) {
// Add all profitable projects that we can afford
while (i < n && projects.get(i).capital <= w) {
maxHeap.add(projects.get(i).profit);
i++;
}
// If no projects can be funded, break out of the loop
if (maxHeap.isEmpty()) {
break;
}
// Otherwise, take the project with the maximum profit
w += maxHeap.poll();
}
return w;
}
}