FunctionRacerProblemsRace

Grow the Fund

HardGreedyPython, JavaScript, TypeScript, Java, C++

You start with capital and may complete at most k projects, one after another. Project i can only be started while you hold at least needs[i] capital, and completing it adds gains[i] to your capital; the capital it needs is not spent. Each project can be completed at most once.

Return the largest capital you can hold after completing at most k projects. The largest capital is unique, so no tie rule is needed.

Example 1

capital 0
k 2
needs [0,1,1]
gains [1,2,3]
returns 4

With 0 capital only project 0 is startable; it brings 1. Then projects 1 and 2 are both startable, and project 2 pays more: 0 + 1 + 3 = 4.

Example 2

capital 0
k 3
needs [0,1,2]
gains [1,2,3]
returns 6

Project 0 unlocks project 1, which unlocks project 2: 0 + 1 + 2 + 3 = 6.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems