Fewest Cabinets for Two-Resource Jobs
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Group jobs with two resource demands into the fewest identical cabinets, using exact search with symmetry breaking and pruning (2D vector bin packing).
A data center must host a set of jobs. Job i needs jobs[i][0] units of the first resource and jobs[i][1] units of the second resource. Jobs are stored in identical cabinets, and each cabinet offers cap_a units of the first resource and cap_b units of the second.
Each job must be placed entirely inside one cabinet; it cannot be split. A cabinet may hold any number of jobs, provided that for each resource separately, the total demand of the jobs in that cabinet is at most the cabinet's capacity for that resource.
Return the smallest number of cabinets that can hold every job.
Special cases
- If there are no jobs, return
0. - If some single job exceeds
cap_ain the first resource orcap_bin the second resource, no valid placement exists, so return-1. - Jobs with zero demand in one or both resources are allowed and are placed like any other job.
Parameters
jobs: a list of[need_a, need_b]pairs.cap_a: the capacity of one cabinet for the first resource.cap_b: the capacity of one cabinet for the second resource.
Returns an integer: the minimum number of cabinets, or -1 if impossible.
Constraints
0 <= len(jobs) <= 20(an exact search is expected, so the job count is small).- Every demand and both capacities are non-negative integers.
- Jobs may be grouped in any way and in any order.
- Identical jobs may appear multiple times.
Examples
Example 1
Input: min_cabinets([[2, 2], [3, 3], [4, 1], [1, 4]], 5, 5)
Output: 2
Example 2
Input: min_cabinets([[2, 2], [2, 2], [2, 2]], 4, 4)
Output: 2
Example 3
Input: min_cabinets([[6, 1], [1, 6]], 5, 5)
Output: -1
Hints
Hint 1
This is NP-hard in general, so with about 20 jobs aim for a well-pruned exhaustive search rather than a polynomial algorithm.
Hint 2
Process jobs one at a time, placing each into an already-open cabinet where it fits or into one new cabinet. Placing the largest jobs first makes dead ends show up early.
Hint 3
Prune hard: stop any branch whose cabinet count already reaches the best answer found, and skip open cabinets whose current load is identical to one you already tried for this job.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Packing vectors into bins is NP-hard, because the one-dimensional case already is. For the small job counts here, an exact backtracking search with strong pruning is the practical choice.
- Impossible case. If any job alone exceeds
cap_aorcap_b, return-1. If there are no jobs, return0. - Ordering. Sort jobs from bulkiest to smallest (by product of demands, then by sum). Large jobs constrain the layout most, so placing them first exposes failures early.
- Search. Take jobs in that order. For each job, try:
- every already-open cabinet where it still fits in both resources;
- opening one new cabinet.
- Upper bound. Start with
best = n, which is always achievable because each job fits alone. Whenever all jobs are placed, updatebestwith the number of open cabinets. - Pruning.
- If the number of open cabinets is already at least
best, abandon the branch, since it cannot improve the answer. - Two open cabinets with the same load are interchangeable for the current job, so only one is tried.
- Only one new empty cabinet is ever opened per job, since all empty cabinets are equivalent.
- If the number of open cabinets is already at least
Why it is correct
Every valid assignment of jobs to cabinets corresponds to a path in this search. Each job either joins a cabinet that already holds earlier jobs or starts a new one. The skipped branches are either symmetric copies of branches already explored (equal loads, multiple empty cabinets) or branches that cannot beat the best known count. So the search never discards an optimal packing, and the final best is the true minimum.
Complexity
The worst case is exponential in n. Without pruning, the number of set partitions is the Bell number. The sorting, symmetry breaking and bounding cut this down enough for around 20 jobs. Extra space is O(n) for the recursion stack and the list of cabinet loads.
Reference solution
from typing import List
def min_cabinets(jobs: List[List[int]], cap_a: int, cap_b: int) -> int:
if any(a > cap_a or b > cap_b for a, b in jobs):
return -1
if not jobs:
return 0
# Place bulky jobs first so that conflicts appear early in the search.
order = sorted(jobs, key=lambda j: (j[0] * j[1], j[0] + j[1]), reverse=True)
n = len(order)
best = n # every job in its own cabinet is always valid
loads = [] # current (used_a, used_b) of each open cabinet
def place(idx):
nonlocal best
if idx == n:
best = min(best, len(loads))
return
if len(loads) >= best:
return
a, b = order[idx]
seen = set()
for i in range(len(loads)):
state = loads[i]
if state in seen:
continue
seen.add(state)
if state[0] + a <= cap_a and state[1] + b <= cap_b:
loads[i] = (state[0] + a, state[1] + b)
place(idx + 1)
loads[i] = state
# Open exactly one new cabinet (all empty cabinets are interchangeable).
loads.append((a, b))
place(idx + 1)
loads.pop()
place(0)
return best