Practice Problem
IPO
Difficulty: Medium
Maximize capital after completing at most k projects by greedily selecting the most profitable affordable projects using a max-heap.
IPO
Suppose you are given k projects to maximize your total capital. You start with an initial capital w. When you complete a project, you earn its pure profit and your capital increases by that amount.
Pick at most k distinct projects from the given list to maximize your final capital.
You are given n projects where the ith project has a pure profit profits[i] and a minimum capital requirement capital[i].
Examples
Example 1:
Input: k = 2, w = 0, profits = [1, 2, 3], capital = [0, 1, 1]
Output: 4
Explanation:
Start with w = 0. Choose project 0 (capital needed: 0, profit: 1). Now w = 1.
Choose project 2 (capital needed: 1, profit: 3). Now w = 4.
Maximum capital = 4.Example 2:
Input: k = 3, w = 0, profits = [1, 2, 3], capital = [0, 1, 2]
Output: 6
Explanation:
Choose project 0 (profit: 1, w = 1), then project 1 (profit: 2, w = 3),
then project 2 (profit: 3, w = 6).Constraints
1 <= k <= 10^50 <= w <= 10^9n == profits.length == capital.length1 <= n <= 10^50 <= profits[i] <= 10^40 <= capital[i] <= 10^9
Expected Complexity
- Time: O(n log n)
- Space: O(n)
MEDIUM
Heap
Max Heap
Priority Queue
Greedy
Sorting
Intermediate
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
This section is available for CodeSnatch Premium members only.
