24 · Heaps & Priority Queues

#502 IPO

Hardsolve on LeetCode ↗

frequently reported atSamsungAmazonGoogle

the problem

In plain words

You may finish at most k distinct projects. Project i can only be started when your capital is at least capital[i]; finishing it adds its pure profit profits[i] to your capital (starting it doesn't spend anything). You begin with capital w. Return the largest capital you can end with.

in k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]

out 4

With 0 only project 0 can start: capital becomes 1. Now projects 1 and 2 are affordable; take the one with profit 3 → 4.

in k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]

out 6

0 → 1 → 3 → 6: each profit unlocks the next project.

  • 1 ≤ k ≤ 10⁵
  • 0 ≤ w ≤ 10⁹
  • 1 ≤ n ≤ 10⁵, n == profits.length == capital.length
  • 0 ≤ profits[i] ≤ 10⁴
  • 0 ≤ capital[i] ≤ 10⁹
  • The answer fits in a 32-bit signed int

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #1046
      Last Stone Weight
      Easyin Heaps
    2. #621
      Task Scheduler
      Mediumin Heaps
    3. #253
      Meeting Rooms II
      Mediumin IntervalsLeetCode Premium