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.
Example 1:
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1] Output: 4 Explanation: Since your initial capital is 0, you can only start the project indexed 0. After finishing it you will obtain profit 1 and your capital becomes 1. With capital 1, you can either start the project indexed 1 or the project indexed 2. Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital. Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
Example 2:
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2] Output: 6
Constraints:
1 <= k <= 105
0 <= w <= 109
n == profits.length
n == capital.length
1 <= n <= 105
0 <= profits[i] <= 104
0 <= capital[i] <= 109
The problem aims to maximize the final capital by selecting at most k
projects, considering each project's profit and required capital. The solution employs a greedy approach using priority queues (heaps) for efficiency.
Approach:
Project Representation: Each project is represented as a pair (capital required, profit).
Priority Queues: Two priority queues are used:
q1
: Stores projects sorted by their capital requirements in ascending order. This allows us to efficiently find the projects we can currently afford.q2
: Stores the profits of the affordable projects, sorted in descending order. This allows us to select the most profitable projects first.Iteration: The algorithm iterates k
times (or until no more projects can be selected). In each iteration:
q1
to q2
that can be afforded with the current capital (w
).q2
is not empty, the algorithm selects the most profitable project from q2
, adds its profit to the current capital (w
), and reduces the remaining project count (k
).Result: The final capital (w
) after processing at most k
projects is the maximized capital.
Time Complexity Analysis:
q1
takes O(n log n) time, where n is the number of projects.k
times. In each iteration, moving elements from q1
to q2
takes O(n log n) in the worst case (all projects might be transferred at once), but on average, it will be less. Popping from q2
is O(1).Space Complexity Analysis:
The space complexity is O(n) to store the priority queues q1
and q2
, which hold at most n
project pairs and profits, respectively.
Code Explanation (Python):
import heapq
class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
projects = sorted(zip(capital, profits)) # Sort projects by capital
available_projects = [] # Priority queue for affordable projects (profits)
index = 0
for _ in range(k): # iterate at most k times
while index < len(projects) and projects[index][0] <= w: #Add all affordable projects
heapq.heappush(available_projects, -projects[index][1]) # Use negative profits for max-heap
index += 1
if not available_projects: # No affordable projects left
break
w -= heapq.heappop(available_projects) #Select the most profitable project
return w
The Python code efficiently utilizes heapq
for heap operations. The projects
list is sorted initially for efficient processing of affordable projects. The negative sign in heapq.heappush
is used because heapq
implements a min-heap, and we need a max-heap for profits.
The Java, C++, and Go code examples follow a very similar logic and structure, using their respective priority queue implementations. The time and space complexity analyses remain consistent across all languages.