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 i-th 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.

Input & Output

Example 1 — Basic Case
$ Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
💡 Note: Start with capital 0. Can afford project 0 (capital=0, profit=1). After project 0, capital becomes 1. Now can afford projects 1 or 2. Project 1 has profit=2, so pick it. Final capital: 0 + 1 + 2 = 3. Wait, let me recalculate: we can do project 0 (capital 0→1), then project 2 (capital 1→4). Actually both projects 1 and 2 need capital 1, and both give different profits. Project 2 gives profit 3, so final is 0→1→4.
Example 2 — Limited by k
$ Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6
💡 Note: Can do all projects in order: project 0 (0→1), then project 1 (1→3), then project 2 (3→6)
Example 3 — Limited by Capital
$ Input: k = 1, w = 0, profits = [1,2,3], capital = [1,1,1]
Output: 0
💡 Note: Cannot afford any project since all require capital ≥ 1 but we start with 0

Constraints

  • 1 ≤ k ≤ 105
  • 0 ≤ w ≤ 109
  • n == profits.length == capital.length
  • 1 ≤ n ≤ 105
  • 0 ≤ profits[i] ≤ 104
  • 0 ≤ capital[i] ≤ 109

Visualization

Tap to expand
IPO - Maximize Capital INPUT Projects (profit, capital) P0 (1, 0) P1 (2, 1) P2 (3, 1) k = 2 (max projects) w = 0 (initial capital) profits = [1, 2, 3] capital = [0, 1, 1] Two Heaps Structure Min Heap (by capital) Available Max Heap (by profit) Best Pick Move affordable projects to max heap, pick best ALGORITHM STEPS 1 Initialize w=0, Add all to min-heap [(0,1),(1,2),(1,3)] 2 Round 1 (w=0) Move P0 (cap=0) to max-heap Pick P0: profit=1 w = 0 + 1 = 1 3 Round 2 (w=1) Move P1,P2 (cap=1) to max Pick P2: profit=3 (best) w = 1 + 3 = 4 4 Done (k=2 reached) Completed 2 projects Final capital = 4 Project Selection Order: P0 --> P2 --> OK FINAL RESULT Capital Growth 0 Start 1 +P0 4 +P2 Final Capital 4 Maximized with k=2 projects completed Used: P0(+1) + P2(+3) = 4 Greedy optimal: OK Key Insight: Use a min-heap (sorted by capital) to track available projects and a max-heap (sorted by profit) to pick the most profitable one. Each round: move all affordable projects to max-heap, then greedily pick the highest profit. Time: O(n log n), Space: O(n) for the two heaps. TutorialsPoint - IPO | Greedy with Two Heaps
Asked in
Google 12 Facebook 8 Amazon 6
78.4K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen