IPO

You may finish at most k distinct projects before an IPO. Project i needs at least capital[i] capital to start and adds a pure profit of profits[i] to your capital when finished. You start with capital w. Pick at most k projects to maximize your final capital and return it.

30:00

IPO
hard
Free written solution
Topics
Companies

You may finish at most k distinct projects before an IPO. Project i needs at least capital[i] capital to start and adds a pure profit of profits[i] to your capital when finished. You start with capital w. Pick at most k projects to maximize your final capital and return it.

Example 1:
Input: {"k":2,"w":0,"profits":[1,2,3],"capital":[0,1,1]}
Output: 4
Constraints:
  • 1≤k≤1051 \leq k \leq 10^5

  • 0≤w≤1090 \leq w \leq 10^9

  • n==profits.length==capital.lengthn == \text{profits.length} == \text{capital.length}

  • 1≤n≤1051 \leq n \leq 10^5

  • 0≤profits[i]≤1040 \leq \text{profits}[i] \leq 10^4

  • 0≤capital[i]≤1090 \leq \text{capital}[i] \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"k":2,"w":0,"profits":[1,2,3],"capital":[0,1,1]}

Sort the 3 projects by required capital, so the ones we can afford always form a prefix of the list.

Capital 0

projects done 0 / k = 2

Projects sorted by capital. Left of w = affordable, right = locked.

w=0

need 0

+1

need 1

+2

need 1

+3

Max-heap of affordable profits (0)

empty

■ in heap■ just unlocked■ finished (#round)■ locked
Variables
VariableValue
k2
w0
i0
round0
heap0
DepthFunction Call
Stack empty
0/9