Maximum Profit Under Capacity Constraint
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Tests understanding of 0/1 knapsack disguised by a misleading ordering constraint that is actually always satisfiable.
You are optimizing a batch processing system. Each job has an associated profit and resource requirement. You are given:
profits: a list whereprofits[i]is the profit earned by completing jobiresources: a list whereresources[i]is the amount of computational resources needed for jobicapacity: the total computational resources available
Your system can execute jobs in any order you choose, as long as you schedule them in non-decreasing order of their resource requirements. In other words, if you select a subset of jobs to run, you must be able to arrange them so that each job's resource requirement is at least as large as the previous job's.
Return the maximum total profit you can achieve by selecting and scheduling a valid subset of jobs whose total resource consumption does not exceed capacity.
Constraints
1 <= len(profits) <= 1000len(profits) == len(resources)0 <= profits[i] <= 10^60 <= resources[i] <= 10^40 <= capacity <= 10^4- All values are integers
Examples
Example 1
Input: max_profit_under_capacity([10, 20, 30], [1, 2, 3], 3)
Output: 30
Example 2
Input: max_profit_under_capacity([5, 10, 15], [3, 2, 1], 3)
Output: 25
Example 3
Input: max_profit_under_capacity([10, 20, 30], [5, 6, 7], 4)
Output: 0
Hints
Hint 1
The scheduling constraint sounds restrictive but is actually trivial: any subset of jobs can be scheduled by sorting them by resource requirement.
Hint 2
Since the ordering constraint is always satisfiable for any subset, the real question is: which subset maximizes profit while staying under capacity?
Hint 3
This is the classic 0/1 knapsack problem. Use dynamic programming with a 1D array, updating from high capacity to low to ensure each job is used at most once.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is recognizing that the scheduling constraint is not actually restrictive. Any subset of jobs can be validly scheduled: simply sort the chosen jobs by their resource requirements in non-decreasing order and execute them in that sequence. This means the constraint is automatically satisfied for every possible subset.
With the scheduling constraint out of the way, the problem reduces to: select a subset of jobs such that their total resource consumption does not exceed capacity and their total profit is maximized. This is precisely the 0/1 knapsack problem, where:
- Each job's profit is the item's value
- Each job's resource requirement is the item's weight
- The capacity is the knapsack size limit
We use dynamic programming with a one-dimensional array. Let dp[r] represent the maximum profit achievable using at most r resources. For each job, we decide whether to include it or not:
- If we skip the job:
dp[r]remains unchanged - If we take the job: we need
resourceunits, so we addprofittodp[r - resource]
The answer is dp[capacity].
Why iterate backwards? When updating dp[r] for a given job, we iterate from capacity down to resource. This ensures that when we read dp[r - resource], it still contains the value from before considering this job, preventing us from using the same job multiple times.
Complexity
- Time: O(n · C), where n is the number of jobs and C is the capacity. We process each job once and update up to C entries in the DP array per job.
- Space: O(C) for the DP array.
Solution
from typing import List
def max_profit_under_capacity(profits: List[int], resources: List[int], capacity: int) -> int:
if capacity <= 0 or not profits:
return 0
dp = [0] * (capacity + 1)
for profit, resource in zip(profits, resources):
if resource < 0 or resource > capacity:
continue
for rem in range(capacity, resource - 1, -1):
dp[rem] = max(dp[rem], dp[rem - resource] + profit)
return dp[capacity]