Skip to content
Decoder

IPO

Hard

Problem

A small company wants to grow its capital before going public. It starts with capital w and can finish at most `k` distinct projects. Project i can only be started when the current capital is at least `capital[i]`, and finishing it adds profits[i] to the capital (the starting capital is not spent).

Pick projects one at a time to maximize the final capital, and return it.

Example 1

Input
k = 2, w = 0, profits = [1, 2, 3], capital = [0, 1, 1]
Output
4

With 0 capital only project 0 is open: do it and reach 1. Now projects 1 and 2 are open: pick project 2 (profit 3) and finish with 4.

Constraints

  • 1 <= k <= 10^5
  • 0 <= w <= 10^9
  • 1 <= n <= 10^5 (n is the number of projects)
  • 0 <= profits[i] <= 10^4
  • 0 <= capital[i] <= 10^9

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.