oneby7

Heaps Simulating with a heap

419. IPO

Hard

A company starts with capital w and can finish at most k projects before it goes public. There are n projects. Project i can start only when the company's capital is at least capital[i], and finishing it adds profits[i] to the capital.

The required capital is a minimum the company must hold. It is not spent, so starting a project never lowers the capital. The company finishes one project at a time, in any order it likes, and each project at most once.

Return the largest capital the company can have after finishing at most k projects.

Example 1

Input: k = 3 w = 1 profits = [2, 5, 1, 4, 3] capital = [0, 3, 1, 2, 6]

Output: 12

Explanation: With capital 1, only projects 0 and 2 can start. Project 0 brings 2, so the capital becomes 3. Now project 1 can start, and it brings 5, for 8. Projects 2, 3 and 4 can all start now, and project 3 brings the most, 4, for 12.

Example 2

Input: k = 4 w = 0 profits = [3, 1, 10] capital = [0, 0, 8]

Output: 4

Explanation: Projects 0 and 1 need nothing and bring the capital to 4. Project 2 needs 8, so no third project can start, even though k allows four.

Example 3

Input: k = 2 w = 5 profits = [10000, 10000, 1] capital = [5, 10005, 1000000000]

Output: 20005

Explanation: Capital equal to the requirement is enough. Project 0 needs exactly 5 and brings the capital to 10005, which is exactly what project 1 needs.

Check your understanding

Work out the answer for this input before you write any code.

k = 2 w = 1 profits = [6, 2, 9, 3] capital = [3, 1, 4, 0]

Constraints

  • 1 ≤ k ≤ 105
  • 0 ≤ w ≤ 109
  • n == profits.length == capital.length
  • 1 ≤ n ≤ 105
  • 0 ≤ profits[i] ≤ 104
  • 0 ≤ capital[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.