#502

IPO

Hard Sort by capital + max-heap of profits

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

You start with capital w and can finish at most k projects. Each project needs some minimum capital to start and adds its profit to your capital when done. Maximise final capital.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: with limited capital, finish up to k projects to maximise money.

Why this works: among projects you can afford right now, always take the most profitable. As money grows, more projects become affordable.

Example: k = 2, w = 0, profits [1, 2, 3], capital [0, 1, 1]

afford project 0 → w = 1now afford 1, 2 → take 3 → w = 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Re-scanning all projects every round — O(n·k).

Solve on LeetCode ↗
Complexity
  • Time: O(n log n + k log n).
  • Space: O(n).
Topic: Heaps & Priority Queue
🎬 Episodes 64–68: video coming soon