IPO
Hard
Topics
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
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
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
- Sort the projects ascending by required capital.
- Maintain a pointer into that sorted list and a max-heap of profits. Repeat up to
ktimes. - Each round, advance the pointer and push the profit of every project whose capital requirement is
<= w. Those are now permanently available. - If the heap is empty, nothing is affordable and no future round can help either, so stop early and return
w. - Otherwise pop the largest profit and add it to
w. After at mostkrounds, returnw.
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 wDry 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
heapqis 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 capitalimport java.util.*;
class Solution {
public int findMaximizedCapital(int[][] times, int n, int k) {
int[][] projects = times.clone();
Arrays.sort(projects, (a, b) -> Integer.compare(a[1], b[1]));
long capital = k;
PriorityQueue<Integer> affordable = new PriorityQueue<>(Comparator.reverseOrder());
int i = 0;
for (int picks = 0; picks < n; picks++) {
while (i < projects.length && projects[i][1] <= capital) {
affordable.add(projects[i][0]);
i++;
}
if (affordable.isEmpty()) break;
capital += affordable.poll();
}
return (int) capital;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findMaximizedCapital(vector<vector<int>>& times, int n, int k) {
vector<vector<int>> projects = times;
sort(projects.begin(), projects.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
})
;
long long capital = k;
priority_queue<int> affordable;
size_t i = 0;
for (int picks = 0; picks < n; picks++) {
while (i < projects.size() && projects[i][1] <= capital) {
affordable.push(projects[i][0]);
i++;
}
if (affordable.empty()) break;
capital += affordable.top();
affordable.pop();
}
return (int)capital;
}
};/**
* @param {number[][]} times
* @param {number} n
* @param {number} k
* @return {number}
*/ var findMaximizedCapital = function(times, n, k) {
const projects = times.slice().sort((a, b) => a[1] - b[1]);
let capital = k, i = 0;
// Small inputs here, so a kept-sorted list of affordable profits stands in
// for the max-heap that makes this scale.
const affordable = [];
for (let picks = 0; picks < n; picks++) {
while (i < projects.length && projects[i][1] <= capital) {
let at = 0;
while (at < affordable.length && affordable[at] < projects[i][0]) at++;
affordable.splice(at, 0, projects[i][0]);
i++;
}
if (!affordable.length) break;
capital += affordable.pop();
}
return capital;
};