Skip to the code
cf.
+50 XP

#576 Fund Projects to Grow Capital

hard

A small design studio starts with capital dollars and can take on at most k projects. Project i needs at least minCapital[i] dollars in the bank to start and adds profit[i] dollars when finished (the starting money isn't spent). Projects are done one at a time, and each can be done once.

Write bestFinalCapital(k, capital, profit, minCapital) that returns the most money the studio can have after finishing at most k projects.

Example 1

Input: k = 2, capital = 0, profit = [1,2,3], minCapital = [0,1,1]
Output: 4

Only project 0 is affordable at first (+1). Then project 2 is affordable (+3), for 4.

Example 2

Input: k = 3, capital = 0, profit = [1,2,3], minCapital = [0,1,2]
Output: 6

Do all three projects in order: 0, then 1, then 2.

Constraints

  • 1 <= k <= 100000
  • 0 <= capital <= 10^9
  • 0 <= profit.length === minCapital.length <= 100000
  • 0 <= profit[i] <= 10^4, 0 <= minCapital[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).