IPO (LeetCode 502): Solution and Explanation

Hard
Array
Greedy
Sorting
Heap

You start with capital w and may finish at most k projects. Project i needs capital[i] to start and adds profits[i] to your capital when done, without spending the requirement. Return the largest capital you can end with.

Intuition

Capital only goes up, because every profit is zero or more and starting a project costs nothing. That means a project you can afford now stays affordable for the rest of the game. The set of options only grows.

With that in mind, the choice each round is easy. Among the projects you can afford right now, take the one with the largest profit. Any other choice leaves you with less capital, and less capital can only unlock the same projects or fewer. A smaller pick never opens a door that the larger pick keeps closed.

The work is in finding that best affordable project quickly as capital rises. Sort projects by required capital, so the affordable ones are always a prefix of the sorted list. A pointer walks forward through that prefix as capital grows, and every project it passes goes into a max-heap keyed by profit. Each round you pop the top of the heap.

If the heap is ever empty, nothing is affordable and nothing will become affordable, since capital cannot grow without finishing a project. You stop early and return what you have.

Approach: Sort by capital + max-heap of profits

  1. Pair each project's capital with its profit and sort the pairs by capital.
  2. Keep a pointer i into the sorted list and an empty max-heap of profits.
  3. Repeat up to k times: while projects[i] needs at most w, push its profit onto the heap and advance i.
  4. If the heap is empty, break: no remaining project is affordable.
  5. Otherwise pop the largest profit and add it to w.
  6. Return w.

Step-by-step example

k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
  1. Sorted by capital the projects are (0, +1), (1, +2), (1, +3).
  2. Round 1: capital is 0, so only (0, +1) is affordable. Push 1. The heap is [1].
  3. Pop 1. Capital becomes 1.
  4. Round 2: capital is 1, so (1, +2) and (1, +3) unlock. The heap is [3, 2].
  5. Pop 3. Capital becomes 4. Both rounds are used, so return 4.

Code: Python, Java, C++

1class Solution:
2    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
3        projects = sorted(zip(capital, profits))
4        heap = []
5        i = 0
6        for _ in range(k):
7            while i < len(projects) and projects[i][0] <= w:
8                heapq.heappush(heap, -projects[i][1])
9                i += 1
10            if not heap:
11                break
12            w -= heapq.heappop(heap)
13        return w
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n + k log n)

Sorting costs O(n log n). Each project is pushed at most once and each round pops at most once, and every heap operation is O(log n).

Space

O(n)

The sorted project list and the heap each hold at most n entries.

Other approaches

Scan for the best affordable project each round (time O(k * n), space O(n))

Every round, loop over all unfinished projects and take the most profitable one you can afford. It is correct and easy to write, but it repeats the full scan k times and times out when both n and k are 100,000.

Two heaps (time O(n log n + k log n), space O(n))

Put all projects in a min-heap by capital instead of sorting, and move them into a max-heap of profits as they become affordable. It has the same complexity as sorting and is a common way to write it in languages with a built-in heap.

Common mistakes

  • Subtracting the required capital when starting a project. Capital is a threshold only; the profit is pure gain.
  • Rebuilding or rescanning the heap every round instead of moving a single pointer forward through the sorted list.
  • Looping k times without the empty-heap check, which either crashes on an empty pop or wastes up to 100,000 rounds doing nothing.
  • Sorting by profit instead of by capital. The pointer needs the affordable projects to form a prefix, which only happens when sorted by capital.

Frequently asked questions

Why is taking the largest affordable profit always safe?

Any other affordable choice leaves you with less capital after the round. Less capital unlocks a subset of what more capital unlocks, so the larger pick is never worse for later rounds.

Why can the algorithm stop early when the heap is empty?

Capital only changes when you finish a project. If nothing is affordable now, capital stays the same, so nothing will ever become affordable.

Does it matter how ties in capital are ordered?

No. All projects with the same requirement become affordable together and go into the heap at the same moment, where the heap orders them by profit.

What is the time complexity of IPO?

O(n log n + k log n): one sort, then at most one push per project and one pop per round, each costing O(log n).