Maximize Hourly Output Revenue for a Three-Stage Assembly Line
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Tests budgeted 0/1 selection with a bottleneck objective, requiring Pareto-frontier pruning over a four-dimensional knapsack state space.
Three-Stage Assembly Line: Maximum Hourly Revenue
You are designing a production pipeline that consists of exactly three stage types: Feeders (type 1 or "F"), Processors (type 2 or "P"), and Sealers (type 3 or "S"). A catalog of available units is provided; each unit belongs to one stage type and has an installation cost and a rated capacity (units produced per hour).
How throughput is determined
The pipeline's actual output rate equals the capacity of its weakest stage — the minimum of the total capacity across all installed Feeders, the total capacity across all installed Processors, and the total capacity across all installed Sealers. If no units are installed for a stage type, that stage's total capacity is zero, so the pipeline produces nothing.
Hourly revenue model
Each unit produced sells at sale_price and incurs a variable_cost to produce. Every installed unit also contributes a recurring hourly ownership cost equal to its installation cost multiplied by amortization_rate. The net hourly profit for a given selection is therefore:
net_margin = sale_price − variable_cost
pipeline_output = min(total_feeder_cap, total_processor_cap, total_sealer_cap)
hourly_profit = net_margin × pipeline_output − amortization_rate × total_installation_cost
Your task
Select a subset of the catalog (each unit used at most once) such that:
- The total installation cost does not exceed
budget. hourly_profitis maximized.
Return the maximum achievable hourly profit as a float. Selecting no units is always allowed, so the answer is never negative.
Input details
units— a list of[unit_type, install_cost, capacity]triples.unit_typemay be the integer1,2, or3, or the string"F"/"Feeder"/"Feeders"(case-insensitive) for type 1,"P"/"Processor"/"Processors"for type 2,"S"/"Sealer"/"Sealers"for type 3.- Units with an unrecognized type, a negative
install_cost, or aninstall_costexceedingbudgetare silently ignored.
budget— a non-negative integer; maximum total installation cost allowed.sale_price— revenue per unit produced (float).variable_cost— production cost per unit produced (float).amortization_rate— fraction of each unit's installation cost charged per hour (float ≥ 0).
Constraints
0 ≤ budget ≤ 10^61 ≤ len(units) ≤ 2 0000 ≤ install_cost ≤ 10^6per unit1 ≤ capacity ≤ 10^4per unit0 ≤ variable_cost ≤ sale_price ≤ 10^40 ≤ amortization_rate ≤ 1- The catalog may contain duplicate units (same type, cost, and capacity); each copy is a distinct physical unit and may be selected independently.
- Zero-cost units are valid and must be considered.
Examples
Example 1
Input: best_pipeline_profit([[1, 10, 10], [2, 10, 10], [3, 10, 10]], 30, 5, 1, 0.1)
Output: 37
Example 2
Input: best_pipeline_profit([[1, 5, 100], [2, 5, 100], [3, 5, 100], [1, 4, 50], [2, 4, 50], [3, 4, 50]], 15, 3, 1, 0.2)
Output: 197
Example 3
Input: best_pipeline_profit([[1, 3, 10], [2, 3, 10], [3, 3, 10]], 8, 10, 10, 1)
Output: 0
Example 4
Input: best_pipeline_profit([[1, 0, 5], [2, 0, 7], [3, 0, 6]], 0, 2, 0, 0)
Output: 10
Hints
Hint 1
The profit formula only depends on four aggregate numbers per selection: total installation cost and the total capacity of each of the three stage types. Two selections with identical aggregates are interchangeable, so your DP state only needs those four numbers.
Hint 2
After processing each unit, prune any state that is weakly dominated: if state A costs no more than state B and has at least as much capacity in every stage type (and is strictly better in at least one dimension), state B can never lead to a better outcome than A and can be dropped.
Hint 3
If the net margin (sale_price − variable_cost) is ≤ 0, the empty selection wins immediately. Also remember that a missing stage type caps pipeline output at zero, so a selection that installs nothing for any one stage type earns zero throughput revenue.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core insight
The hourly profit formula depends on only four numbers derived from any selection: the total installation cost and the summed capacity of each of the three stage types. Two selections that share the same four-tuple are interchangeable for all future decisions. This lets us run a set-based DP whose states are exactly those four-tuples, rather than tracking which specific units were chosen.
Algorithm
- Start with a single state representing the empty selection:
(cost=0, feeder=0, processor=0, sealer=0). - For each valid unit, compute every state reachable by adding it to an existing state whose running cost stays within
budget. Merge those new states with the current set. - Prune dominated states. State A dominates state B when A costs no more than B and has at least as much capacity in every stage type, with at least one coordinate strictly better. B can safely be dropped: any future addition that keeps B feasible also keeps A feasible, and A's pipeline output is always at least as high.
- After processing all units, evaluate
margin × min(f, p, s) − rate × costfor every surviving state. Return the maximum value, floored at0.0(the empty selection).
Why it is correct
The bottleneck function min(f, p, s) is non-decreasing in each capacity argument, and the amortization term is non-increasing in cost. So for any extension of a set of units we might add later, dominating state A will always produce profit ≥ that of dominated state B. Pruning never removes the globally optimal selection from consideration. Without pruning the DP enumerates all subsets; with pruning it retains a representative that is at least as good as every subset.
When margin ≤ 0, all non-empty selections yield ≤ 0 profit, and the best = 0.0 initialization handles that correctly.
Edge cases
- No affordable units for some stage type: that stage's capacity stays 0,
min(...)is 0, profit is ≤ 0, answer is0.0. - Zero-cost units: always survive dominance pruning because they are free capacity.
- Duplicate catalog entries: each is an independent unit; both may be selected.
margin ≤ 0: answer is0.0(empty selection wins).
Complexity
Let S be the maximum Pareto frontier size and n the number of valid units.
- Time:
O(n · S²)— each unit at most doubles the candidate set to2S, and the pairwise dominance check is quadratic in the candidate count. - Space:
O(S)for the frontier.
S is bounded in practice by 4-D dominance; in the worst case it is pseudo-polynomial in budget and total capacity, but the pruning keeps it manageable for the given constraints.
Reference solution
from typing import List
def best_pipeline_profit(
units: List[List],
budget: int,
sale_price: float,
variable_cost: float,
amortization_rate: float,
) -> float:
type_index = {
1: 0, "F": 0, "FEEDER": 0, "FEEDERS": 0,
2: 1, "P": 1, "PROCESSOR": 1, "PROCESSORS": 1,
3: 2, "S": 2, "SEALER": 2, "SEALERS": 2,
}
states = {(0, 0, 0, 0)}
for unit_type, install_cost, capacity in units:
idx = type_index.get(unit_type)
if idx is None and isinstance(unit_type, str):
idx = type_index.get(unit_type.upper())
if idx is None or install_cost < 0 or install_cost > budget:
continue
additions = set()
for state in states:
cost, f_cap, p_cap, s_cap = state
new_cost = cost + install_cost
if new_cost > budget:
continue
caps = [f_cap, p_cap, s_cap]
caps[idx] += capacity
additions.add((new_cost, caps[0], caps[1], caps[2]))
candidates = list(states | additions)
kept = set()
for i, s in enumerate(candidates):
c, f, p, sl = s
dominated = False
for j, o in enumerate(candidates):
if i == j:
continue
oc, of, op, osl = o
if (oc <= c and of >= f and op >= p and osl >= sl and
(oc < c or of > f or op > p or osl > sl)):
dominated = True
break
if not dominated:
kept.add(s)
states = kept
margin = sale_price - variable_cost
best = 0.0
for cost, f_cap, p_cap, s_cap in states:
output = min(f_cap, p_cap, s_cap)
profit = margin * output - amortization_rate * cost
if profit > best:
best = profit
return best