Skip to content
Work Free practice Coding course Blog Method Results Why me About Enquire Book a call

Coding · Heap (priority queue)

Most capital from at most k projects

Hard · Target 25 minutes · Heap (priority queue) · Type asked atAmazon

You start with capital w and may complete at most k projects, one at a time. Project i can be started only if your current capital is at least capital[i]; completing it adds profits[i] to your capital (the starting requirement is not spent). Each project can be done at most once. Return the largest capital you can end with.

Examples

InputOutputWhy
k = 2, w = 0, profits = [1, 2, 3], capital = [0, 1, 1]4Do project 0 (capital 1), then project 2 (capital 4).
k = 1, w = 0, profits = [1, 2, 3], capital = [1, 1, 2]0Nothing is affordable at the start.
k = 2, w = 1, profits = [5, 4, 1], capital = [1, 2, 6]10Project 0 unlocks project 1; project 2 is never worth it.

Constraints

  • 1 ≤ k ≤ 10⁵
  • 0 ≤ w ≤ 10⁹
  • 1 ≤ n ≤ 10⁵
  • 0 ≤ profits[i] ≤ 10⁴
  • 0 ≤ capital[i] ≤ 10⁹

Hints

Hint 1

Capital never decreases. So once a project becomes affordable, it stays affordable. What does that let you do with the list of projects?

Hint 2

At each step, among the projects you can afford, which one should you do? Why can taking it never hurt later?

Hint 3

Sort projects by capital required and walk a pointer along them as your capital grows, pushing each newly affordable profit into a max-heap. Each round, pop the largest profit.

Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.