Heaps Simulating with a heap
419. IPO
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]
12Explanation: 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]
4Explanation: 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]
20005Explanation: 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.
Constraints
1 ≤ k ≤ 1050 ≤ w ≤ 109n == profits.length == capital.length1 ≤ n ≤ 1050 ≤ profits[i] ≤ 1040 ≤ capital[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.