hardHeapGreedyHeap / Priority Queue

IPO

hardTime: O(n log n)Space: O(n)

Signals to notice

maximize capital after k investmentstwo heaps: affordable and profitablegreedy selection

Brute force first

Try all combinations — O(C(n,k)).

The key insight

Min-heap by capital, max-heap by profit. Each round: move newly affordable projects to profit heap, pick most profitable. O(n log n).

What must stay true

As capital grows, more projects become affordable. The profit heap always picks the best available option.

Easy way to go wrong

Not pre-sorting by capital — the min-heap feeds the profit heap efficiently as budget increases.

Heap / Priority Queue Pattern