Skip to main content
LeetCode 502, Hard. Topics: Array, Greedy, Sorting, Heap (Priority Queue). View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

Suppose LeetCode will start its IPO soon. To sell a good price of its shares, it can only finish at most k distinct projects before the IPO. Help LeetCode maximize its total capital. You are given n projects where the ith project has a pure profit profits[i] and a minimum capital capital[i] is needed to start it. Initially, you have w capital. When you finish a project, you obtain its pure profit, which is added to your total capital. Pick a list of at most k distinct projects to maximize your final capital, and return the final maximized capital.

Examples

Constraints

  • 1 <= k <= 10^5
  • 0 <= w <= 10^9
  • n == profits.length
  • n == capital.length
  • 1 <= n <= 10^5
  • 0 <= profits[i] <= 10^4
  • 0 <= capital[i] <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026