Show problems as
Classic problem #502HardArrayGreedySortingHeap (Priority Queue)

IPO

Real-world version: Workshop Job Selection
Tracked in this browser only
Read IPO on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A repair workshop has a board of n jobs it could take on and enough staff to finish at most max_jobs of them. Job i pays payouts[i] once it is delivered, but the shop will only be handed the job if it already has at least setup[i] in the bank, because that is the value of loan equipment the job needs.

The shop starts with funds in the bank. Taking a job does not spend anything: the setup figure is only a threshold the bank balance has to clear, and the payout is added to the balance as soon as the job is finished. Jobs are worked one after another, so a payout is available before choosing the next job, and each job can be taken at most once.

Choose at most max_jobs jobs to end up with as much money in the bank as possible, and return that final balance. The shop may also stop early, or take no jobs at all, if nothing on the board is within reach.

Examples

Example 1

Input
max_jobs = 2, funds = 3, payouts = [5, 8, 2], setup = [0, 6, 3]
Output
16

With 3 in the bank, jobs 0 and 2 are within reach. Taking job 0 pays 5 and brings the balance to 8, which now clears job 1's threshold of 6. Taking job 1 pays 8 for a final balance of 16, and that is two jobs.

Example 2

Input
max_jobs = 1, funds = 0, payouts = [4, 9], setup = [0, 1]
Output
4

An empty bank only clears job 0's threshold of 0, so the single job taken pays 4.

Example 3

Input
max_jobs = 4, funds = 2, payouts = [3, 1, 7], setup = [2, 0, 12]
Output
6

Job 0 pays 3, taking the balance to 5, and job 1 pays 1, taking it to 6. Job 2 needs 12 in the bank, so the shop stops after two of its four allowed jobs.

Example 4

Input
max_jobs = 3, funds = 0, payouts = [6, 5], setup = [1, 2]
Output
0

Both thresholds are above the starting balance of 0, so no job can be handed over and the balance stays at 0.

Constraints

  • 1 <= max_jobs <= 10^5
  • 0 <= funds <= 10^9
  • n == payouts.length
  • n == setup.length
  • 1 <= n <= 10^5
  • 0 <= payouts[i] <= 10^4
  • 0 <= setup[i] <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def max_funds(max_jobs: int, funds: int, payouts: list[int], setup: list[int]) -> int:
Java
public int maxFunds(int maxJobs, int funds, int[] payouts, int[] setup)