IPO
Problem Statement
You can finish at most k projects, one after another, and each project at most once. Project i pays a profit of profits[i]. To start project i you need at least capital[i] capital. You start with w capital, and finishing a project adds its profit to your capital.
Choose the projects so that your final capital is as large as possible, and return that capital.
The answer always fits in a 32-bit signed integer.
k, the most projects you may finish, and w, your starting capital. Then profits and capital, two arrays of the same length.
The largest final capital you can reach.
With 0 capital only project 0 (needs 0) can start. Finish it and your capital is 1. Projects 1 and 2 both need 1, so both are open. Project 2 pays 3, so do it: 1 + 3 = 4. That is two projects, the limit.
Project 0 first: capital 1. Then project 1 (needs 1) pays 2, so capital 3. Then project 2 (needs 2) pays 3, so capital 6.
- • 1 ≤ k ≤ 10^5
- • 0 ≤ w ≤ 10^9
- • n == profits.length
- • n == capital.length
- • 1 ≤ n ≤ 10^5
- • 0 ≤ profits[i] ≤ 10^4
- • 0 ≤ capital[i] ≤ 10^9
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Each round, check every unused project for the largest profit you can afford
Sort by capital; each round push the newly affordable profits, then pop the largest
In each round, scan all unused projects and take the largest profit whose capital is at most your current capital. Add that profit to your capital, mark the project as used, and repeat k times. Stop early if no project is affordable.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Heaps translate into production architectures and low-level memory mechanics.