Profitable Schemes Problem
Description
LeetCode Problem 879.
There is a group of n members, and a list of various crimes they could commit. The i^th crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can't participate in another crime.
Let's call a profitable scheme any subset of these crimes that generates at least minProfit profit, and the total number of members participating in that subset of crimes is at most n. Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: n = 5, minProfit = 3, group = [2,2], profit = [2,3]
Output: 2
Explanation: To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1.
In total, there are 2 schemes.
Example 2:
Input: n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: To make a profit of at least 5, the group could commit any crimes, as long as they commit one.
There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).
Constraints:
- 1 <= n <= 100
- 0 <= minProfit <= 100
- 1 <= group.length <= 100
- 1 <= group[i] <= 100
- profit.length == group.length
- 0 <= profit[i] <= 100
Solution
Approach: 0/1 Knapsack DP with Capped Profit
Trying every subset of crimes is O(2m). This is a knapsack: each crime is taken or not, and it uses group[i] people (the capacity is n) and adds profit[i].
The key trick is the profit dimension. We only care whether profit reaches minProfit, not by how much, so any profit of minProfit or more is stored as exactly minProfit. That keeps the table at (n + 1) × (minProfit + 1) cells.
Let ways[members][earned] be the number of schemes, among the crimes seen so far, that use exactly members people and earn earned (capped).
- Start with
ways[0][0] = 1: the empty scheme. - For each crime with
needpeople andgainprofit, for everymembersfromndown toneedand everyearned:ways[members][min(minProfit, earned + gain)] += ways[members - need][earned]. - The answer is the sum of
ways[members][minProfit]over allmembersfrom0ton, modulo 109 + 7.
The detail to get right is the loop order: members must go down. Each update reads the row members - need, which is smaller, so going down means that row still holds the counts from before this crime, and no crime is used twice. When minProfit = 0, the empty scheme counts too, which this setup handles on its own.
Complexity: O(m · n · minProfit) time and O(n · minProfit) space, where m is the number of crimes.
class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
MOD = 10**9 + 7
# ways[members][earned]: schemes using exactly `members` people whose profit is `earned`,
# where every profit >= minProfit is stored as minProfit.
ways = [[0] * (minProfit + 1) for _ in range(n + 1)]
ways[0][0] = 1 # the empty scheme
for need, gain in zip(group, profit):
# Go down in members so each crime is used at most once (0/1 knapsack).
for members in range(n, need - 1, -1):
for earned in range(minProfit + 1):
capped = min(minProfit, earned + gain)
ways[members][capped] = (ways[members][capped] + ways[members - need][earned]) % MOD
return sum(ways[members][minProfit] for members in range(n + 1)) % MOD
Now write it from memory
Close the write-up and solve it yourself against the tests.