Skip to content
BytePatterns

Capital Project Picks

HardTwo Heaps & K-Way Merge#two-heaps#greedy~40m

Problem

You start with some capital and may run at most rounds projects, one after another. Each project is a pair (cost, profit): you can only start it if your current capital is at least its cost, and finishing it adds its profit to your capital. Costs are never spent — they are only the bar you must clear. Return the capital you end with when you choose greedily for the largest final amount.

Examples

Input:  projects = [(0, 1), (1, 2), (2, 3)], capital = 0, rounds = 2
Output: 3
Why:    only (0, 1) is affordable, giving 1; then (1, 2) is, giving 3
Input:  projects = [(1, 3), (1, 4), (2, 9)], capital = 1, rounds = 2
Output: 14
Why:    take the profit of 4 first, which unlocks the project paying 9
Input:  projects = [(2, 5)], capital = 1, rounds = 1
Output: 1
Why:    edge case, nothing is affordable and the answer is the starting capital

Hints

0 / 3

Stuck on the idea rather than the code? Two Heaps: Running Median covers it.