IPO

Hard

Topics
ArrayGreedyHeapSorting

You are given a list of projects where projects[i] = [profit, capital], an integer k, and a starting capital w.

You may pick at most k distinct projects. A project can only be started if your current capital is at least its required capital, and finishing it adds its profit to your capital.

Return the maximum capital you can finish with.

The original problem passes two parallel arrays plus k and w. This judge packs each project into a [profit, capital] row so it fits the grid harness — the input box labels the three arguments generically, but they are the projects grid, then k, then w.

Example 1

01012123101
2D grid — one row per line.
Input:  projects = [[1,0],[2,1],[3,1]], k = 2, w = 0
Output: 4
Explanation: Start project 0 for profit 1 (capital becomes 1), then project 2 for profit 3, ending at 4.

Example 2

01012123101
Input:  projects = [[1,0],[2,1],[3,2]], k = 3, w = 0
Output: 6

Constraints

  • 1 <= k <= 10^5
  • 0 <= w <= 10^9
  • 1 <= projects.length <= 10^5
  • 0 <= profit, capital <= 10^9
Editorial — intuition, approach, complexity and pitfalls

Intuition

Capital only ever grows, so a project that is affordable now stays affordable forever. That monotonicity makes the greedy choice safe: among everything you can currently afford, always take the largest profit. Sort projects by capital requirement so you can unlock them in order, and keep the unlocked ones in a max-heap keyed by profit.

Approach

  1. Sort the projects ascending by required capital.
  2. Maintain a pointer into that sorted list and a max-heap of profits. Repeat up to k times.
  3. Each round, advance the pointer and push the profit of every project whose capital requirement is <= w. Those are now permanently available.
  4. If the heap is empty, nothing is affordable and no future round can help either, so stop early and return w.
  5. Otherwise pop the largest profit and add it to w. After at most k rounds, return w.
import heapq

class Solution:
    def findMaximizedCapital(self, projects, k, w):
        order = sorted(projects, key=lambda p: p[1])
        heap = []
        i = 0
        n = len(order)
        for _ in range(k):
            while i < n and order[i][1] <= w:
                heapq.heappush(heap, -order[i][0])
                i += 1
            if not heap:
                break
            w += -heapq.heappop(heap)
        return w

Dry Run

projects sorted by capital requirement: [[1, 0], [2, 1], [3, 1]], pick n=2, start k=0

capital=0  i=0
round 1    [1, 0] needs 0 <= 0, affordable    affordable=[-1]  i=1
           [2, 1] needs 1 > 0, stop unlocking
           pop the largest profit, 1          capital=1
round 2    [2, 1] needs 1 <= 1, affordable    affordable=[-2]  i=2
           [3, 1] needs 1 <= 1, affordable    affordable=[-3, -2]  i=3
           pop the largest profit, 3          capital=4

answer 4

Note that i never resets. Sorting by capital means the unlock pointer only ever
moves forward, so across all n rounds each project is pushed at most once --
which is why this is O(n log n) rather than rescanning the list every round.
The heap is ordered by PROFIT while the list is ordered by COST, and needing
both orderings at once is the whole reason two structures appear.

Complexity

  • Time — O(n log n + k log n) — one sort plus heap operations.
  • Space — O(n) for the sorted list and the heap.

Common Pitfalls

  • Sorting by profit instead of by capital breaks the unlock order and lets you push projects you cannot yet afford.
  • Do not re-scan the whole project list every round. The pointer must never move backwards, or the run degrades to O(n x k).
  • Break out when the heap is empty. Continuing wastes rounds, and in some formulations re-pushes already-taken projects.
  • Python's heapq is a min-heap, so negate profits to simulate a max-heap.
  • Each project may be taken at most once. Pop it out of the heap rather than peeking.

Full Solution — all languages

class Solution:
    def findMaximizedCapital(self, times, n, k):
        import heapq
        projects = sorted(times, key=lambda p: p[1])
        capital = k
        affordable = []
        i = 0
        for _ in range(n):
            while i < len(projects) and projects[i][1] <= capital:
                heapq.heappush(affordable, -projects[i][0])
                i += 1
            if not affordable:
                break
            capital += -heapq.heappop(affordable)
        return capital
Asked at Samsung
SensAI
Beta
Tap the mic to stop voice mode