Below is the implementation of the profitable schemes:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/** * Find the number of profitable schemes. * 3D Dynamic Programming approach. * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemes(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 3D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0) ) ); dp[0][0][0] = 1; // Base case for (let i = 1; i <= m; i++) { const g = group[i - 1]; // Members required for the current crime const p = profit[i - 1]; // Profit generated by the current crime for (let j = 0; j <= n; j++) { for (let k = 0; k <= minProfit; k++) { // Exclude the crime dp[i][j][k] = dp[i - 1][j][k]; // Include the crime if we have enough members if (j >= g) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - g][Math.max(0, k - p)]) % MOD; } } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[m][j][minProfit]) % MOD; } return result;} /** * Find the number of profitable schemes. * 2D Dynamic Programming approach (Space Optimization). * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemesOptimized(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 2D DP array const dp = Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0)); dp[0][0] = 1; // Base case for (let i = 0; i < m; i++) { const g = group[i]; // Members required for the current crime const p = profit[i]; // Profit generated by the current crime for (let j = n; j >= g; j--) { for (let k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - g][Math.max(0, k - p)]) % MOD; } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[j][minProfit]) % MOD; } return result;} // Test casesconsole.log(profitableSchemes(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemes(10, 5, [2, 3, 5], [6, 7, 8])); // 7 console.log(profitableSchemesOptimized(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemesOptimized(10, 5, [2, 3, 5], [6, 7, 8])); // 7
Let's break down the implementation:
Implement the profitable schemes solution in different programming languages.
Below is the implementation of the profitable schemes in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/** * Find the number of profitable schemes. * 3D Dynamic Programming approach. * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemes(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 3D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0) ) ); dp[0][0][0] = 1; // Base case for (let i = 1; i <= m; i++) { const g = group[i - 1]; // Members required for the current crime const p = profit[i - 1]; // Profit generated by the current crime for (let j = 0; j <= n; j++) { for (let k = 0; k <= minProfit; k++) { // Exclude the crime dp[i][j][k] = dp[i - 1][j][k]; // Include the crime if we have enough members if (j >= g) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - g][Math.max(0, k - p)]) % MOD; } } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[m][j][minProfit]) % MOD; } return result;} /** * Find the number of profitable schemes. * 2D Dynamic Programming approach (Space Optimization). * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemesOptimized(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 2D DP array const dp = Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0)); dp[0][0] = 1; // Base case for (let i = 0; i < m; i++) { const g = group[i]; // Members required for the current crime const p = profit[i]; // Profit generated by the current crime for (let j = n; j >= g; j--) { for (let k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - g][Math.max(0, k - p)]) % MOD; } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[j][minProfit]) % MOD; } return result;} // Test casesconsole.log(profitableSchemes(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemes(10, 5, [2, 3, 5], [6, 7, 8])); // 7 console.log(profitableSchemesOptimized(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemesOptimized(10, 5, [2, 3, 5], [6, 7, 8])); // 7
First, understand that we need to find the number of subsets of crimes that generate at least minProfit profit and require at most n members.
Define the state dp[i][j][k] as the number of schemes when considering the first i crimes, using exactly j members, and generating at least k profit.
For each crime, we have two options: include it or exclude it. The recurrence relation is dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-group[i-1]][max(0, k-profit[i-1])] if j >= group[i-1].
Set dp[0][0][0] = 1 as the base case, which means there is one way to achieve 0 profit with 0 members when considering 0 crimes (by doing nothing).
Calculate the final answer as the sum of dp[m][j][minProfit] for all j from 0 to n, modulo 10^9 + 7.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the profitable schemes problem using a different approach than shown above.
Handle the case where there are no crimes (return 1 if minProfit is 0, otherwise return 0).
Handle the case where there are not enough members to commit any crime (return 1 if minProfit is 0, otherwise return 0).
Handle the case where the minimum profit required is 0 (return 2^m, where m is the number of crimes, if there are enough members).
Below is the implementation of the profitable schemes:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/** * Find the number of profitable schemes. * 3D Dynamic Programming approach. * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemes(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 3D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0) ) ); dp[0][0][0] = 1; // Base case for (let i = 1; i <= m; i++) { const g = group[i - 1]; // Members required for the current crime const p = profit[i - 1]; // Profit generated by the current crime for (let j = 0; j <= n; j++) { for (let k = 0; k <= minProfit; k++) { // Exclude the crime dp[i][j][k] = dp[i - 1][j][k]; // Include the crime if we have enough members if (j >= g) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - g][Math.max(0, k - p)]) % MOD; } } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[m][j][minProfit]) % MOD; } return result;} /** * Find the number of profitable schemes. * 2D Dynamic Programming approach (Space Optimization). * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemesOptimized(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 2D DP array const dp = Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0)); dp[0][0] = 1; // Base case for (let i = 0; i < m; i++) { const g = group[i]; // Members required for the current crime const p = profit[i]; // Profit generated by the current crime for (let j = n; j >= g; j--) { for (let k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - g][Math.max(0, k - p)]) % MOD; } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[j][minProfit]) % MOD; } return result;} // Test casesconsole.log(profitableSchemes(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemes(10, 5, [2, 3, 5], [6, 7, 8])); // 7 console.log(profitableSchemesOptimized(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemesOptimized(10, 5, [2, 3, 5], [6, 7, 8])); // 7
Let's break down the implementation:
Implement the profitable schemes solution in different programming languages.
Below is the implementation of the profitable schemes in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/** * Find the number of profitable schemes. * 3D Dynamic Programming approach. * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemes(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 3D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0) ) ); dp[0][0][0] = 1; // Base case for (let i = 1; i <= m; i++) { const g = group[i - 1]; // Members required for the current crime const p = profit[i - 1]; // Profit generated by the current crime for (let j = 0; j <= n; j++) { for (let k = 0; k <= minProfit; k++) { // Exclude the crime dp[i][j][k] = dp[i - 1][j][k]; // Include the crime if we have enough members if (j >= g) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - g][Math.max(0, k - p)]) % MOD; } } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[m][j][minProfit]) % MOD; } return result;} /** * Find the number of profitable schemes. * 2D Dynamic Programming approach (Space Optimization). * * @param {number} n - Number of members * @param {number} minProfit - Minimum profit required * @param {number[]} group - Number of members required for each crime * @param {number[]} profit - Profit generated by each crime * @return {number} - Number of profitable schemes */function profitableSchemesOptimized(n, minProfit, group, profit) { const MOD = 1e9 + 7; const m = group.length; // Initialize 2D DP array const dp = Array(n + 1).fill().map(() => Array(minProfit + 1).fill(0)); dp[0][0] = 1; // Base case for (let i = 0; i < m; i++) { const g = group[i]; // Members required for the current crime const p = profit[i]; // Profit generated by the current crime for (let j = n; j >= g; j--) { for (let k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - g][Math.max(0, k - p)]) % MOD; } } } // Calculate the final answer let result = 0; for (let j = 0; j <= n; j++) { result = (result + dp[j][minProfit]) % MOD; } return result;} // Test casesconsole.log(profitableSchemes(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemes(10, 5, [2, 3, 5], [6, 7, 8])); // 7 console.log(profitableSchemesOptimized(5, 3, [2, 2], [2, 3])); // 2console.log(profitableSchemesOptimized(10, 5, [2, 3, 5], [6, 7, 8])); // 7
First, understand that we need to find the number of subsets of crimes that generate at least minProfit profit and require at most n members.
Define the state dp[i][j][k] as the number of schemes when considering the first i crimes, using exactly j members, and generating at least k profit.
For each crime, we have two options: include it or exclude it. The recurrence relation is dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-group[i-1]][max(0, k-profit[i-1])] if j >= group[i-1].
Set dp[0][0][0] = 1 as the base case, which means there is one way to achieve 0 profit with 0 members when considering 0 crimes (by doing nothing).
Calculate the final answer as the sum of dp[m][j][minProfit] for all j from 0 to n, modulo 10^9 + 7.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the profitable schemes problem using a different approach than shown above.
Handle the case where there are no crimes (return 1 if minProfit is 0, otherwise return 0).
Handle the case where there are not enough members to commit any crime (return 1 if minProfit is 0, otherwise return 0).
Handle the case where the minimum profit required is 0 (return 2^m, where m is the number of crimes, if there are enough members).