Profitable Schemes - Memoization step by step
Problem Statement:
There is a group of n
members, and a list of various crimes they could commit. The ith
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 109 + 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:
For each crime, we can take it or not take it in the scheme. So there are $2^m$ unique schemes possible, where $m$ is the number of crimes. If we do take it, we are left with $n-G[i]$ people, where G[i]
is the size of group needed for ith crime and the profit becomes $p+P[i]$. If we do not take it, we have $n$ people and $p$ profit. We can have a recursive solution like this.
int profitableSchemes(int n, int minProfit, vector<int>& groups, vector<int>& profits, int i=0, int p=0)
{
if (i==groups.size()) return (p>=minProfit)?1:0;
int cur = 0;
cur += profitableSchemes(n, minProfit, groups, profits, i+1, p);
if (n-groups[i]>=0)
cur += profitableSchemes(n-groups[i], minProfit, groups, profits, i+1, p+profits[i]);
return cur;
}
We can write the same solution in a different way in order to memoise it later.
int dfs(int i, int n, int p, vector<int>&groups, vector<int>&profits, int minProfit)
{
if (i==groups.size()) return (p>=minProfit)?1:0;
int cur = 0;
cur += dfs(i+1, n, p, groups, profits, minProfit);
if (n-groups[i]>=0)
cur += dfs(i+1, n-groups[i], p+profits[i], groups, profits, minProfit);
return cur;
}
int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit)
{
return dfs(0, n, 0, group, profit, minProfit);
}
We can add memoization to make it faster. Also add mod.
int mod = 1e9+7;
int dfs(int i, int n, int p, vector<int>&groups, vector<int>&profits, int minProfit, vector<vector<vector<int>>>&dp)
{
if (i==groups.size()) return (p>=minProfit)?1:0;
if (dp[i][n][p]!=-1) return dp[i][n][p];
int cur = 0;
cur += dfs(i+1, n, p, groups, profits, minProfit, dp) %mod;
if (n-groups[i]>=0)
cur += dfs(i+1, n-groups[i], min(minProfit, p+profits[i]), groups, profits, minProfit, dp)\
%mod;
return dp[i][n][p] = cur%mod;
}
int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit)
{
vector<vector<vector<int>>> dp(group.size()+1, vector<vector<int>>(n+1, \
vector<int>(minProfit+1,-1)));
return dfs(0, n, 0, group, profit, minProfit, dp);
}
TC: $O(NMP)$, SC: $O(NMP)$ where $N$ is number of people, $M$ is the number of crimes, $P$ is minProfit
.