IPO

Hard#502best of the affordable

You start with capital w and may finish at most k projects, one after another. Project n needs capital[n] to start, and once finished it adds profits[n] to the capital; nothing is spent. Return the largest capital you can end with. Our example: k = 3, w = 1, profits [4, 2, 5, 1, 3, 1], capital [3, 0, 6, 1, 2, 4] — the answer is 12: the projects with profits 2, 4 and 5, in that order.

input
k: how many projects you may finish at most; w: the capital you start with; project i needs capital capital[i] to start and adds profits[i] to your capital when done.
returns
The largest capital you can have after finishing at most k different projects one after another. A project can start when your capital is at least capital[i]; starting it costs nothing, the capital only grows.
constraints
  • 1 ≤ k ≤ 10⁴
  • 0 ≤ w ≤ 10⁹
  • 1 ≤ profits.length ≤ 5000
  • capital.length = profits.length
  • 0 ≤ profits[i] ≤ 10⁴
  • 0 ≤ capital[i] ≤ 10⁹
our 15 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number} k
 * @param {number} w
 * @param {number[]} profits
 * @param {number[]} capital
 * @return {number}
 */
function findMaximizedCapital(k, w, profits, capital) {

}
Ctrl ↵
format
reset

saved in this browser

15 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more heap and top k problems

Pattern “Heap and top K” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.