KatInterview

911. Profitable Schemes

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

This problem asks for the number of ways to choose a subset of "crimes" that meet two criteria: the total members required is at most n, and the total profit generated is at least minProfit. This is a classic combinatorial counting problem that can be modeled as a 0/1 knapsack-style dynamic programming problem. We have two constraints (knapsack capacities): the maximum number of members and the minimum profit.

Solution

Profitable Schemes in Python

class Solution:
    def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
        MOD = 10**9 + 7
        
        # dp[i][j] will be the number of schemes using i members to generate profit j.
        # The j=minProfit column is special: it stores schemes with profit >= minProfit.
        dp = [[0] * (minProfit + 1) for _ in range(n + 1)]
        
        # Base case: There's one way to have 0 members and 0 profit (the empty scheme).
        dp[0][0] = 1
        
        # Iterate through each crime.
        for g, p in zip(group, profit):
            # Iterate backwards for members and profits to use results from the previous state.
            for i in range(n, g - 1, -1):
                for j in range(minProfit, -1, -1):
                    # For each scheme with i-g members and j profit, we can add the current crime.
                    # The new scheme will have i members and a new profit.
                    new_profit = min(minProfit, j + p)
                    dp[i][new_profit] = (dp[i][new_profit] + dp[i - g][j]) % MOD
        
        # The total number of profitable schemes is the sum of schemes with at least minProfit
        # for any number of members from 0 to n.
        total_profitable_schemes = 0
        for i in range(n + 1):
            total_profitable_schemes = (total_profitable_schemes + dp[i][minProfit]) % MOD
            
        return total_profitable_schemes

Profitable Schemes in Java

class Solution {
    public int profitableSchemes(int n, int minProfit, int[] group, int[] profit) {
        int MOD = 1_000_000_007;
        // dp[i][j] will be the number of schemes using i members to generate profit j.
        // The j=minProfit column is special: it stores schemes with profit >= minProfit.
        int[][] dp = new int[n + 1][minProfit + 1];
        
        // Base case: There's one way to have 0 members and 0 profit (the empty scheme).
        dp[0][0] = 1;
        
        // Iterate through each crime.
        for (int i = 0; i < group.length; i++) {
            int g = group[i];
            int p = profit[i];
            
            // Iterate backwards for members and profits to use results from the previous state.
            for (int j = n; j >= g; j--) {
                for (int k = minProfit; k >= 0; k--) {
                    // For each scheme with j-g members and k profit, we can add the current crime.
                    // The new scheme will have j members and a new profit.
                    int newProfit = Math.min(minProfit, k + p);
                    dp[j][newProfit] = (dp[j][newProfit] + dp[j - g][k]) % MOD;
                }
            }
        }
        
        // The total number of profitable schemes is the sum of schemes with at least minProfit
        // for any number of members from 0 to n.
        long totalProfitableSchemes = 0;
        for (int j = 0; j <= n; j++) {
            totalProfitableSchemes = (totalProfitableSchemes + dp[j][minProfit]) % MOD;
        }
        
        return (int) totalProfitableSchemes;
    }
}

Profitable Schemes in C++

class Solution {
public:
    int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) {
        int MOD = 1e9 + 7;
        // dp[i][j] will be the number of schemes using i members to generate profit j.
        // The j=minProfit column is special: it stores schemes with profit >= minProfit.
        vector<vector<int>> dp(n + 1, vector<int>(minProfit + 1, 0));
        
        // Base case: There's one way to have 0 members and 0 profit (the empty scheme).
        dp[0][0] = 1;
        
        // Iterate through each crime.
        for (int i = 0; i < group.size(); ++i) {
            int g = group[i];
            int p = profit[i];
            
            // Iterate backwards for members and profits to use results from the previous state.
            for (int j = n; j >= g; --j) {
                for (int k = minProfit; k >= 0; --k) {
                    // For each scheme with j-g members and k profit, we can add the current crime.
                    // The new scheme will have j members and a new profit.
                    int new_profit = min(minProfit, k + p);
                    dp[j][new_profit] = (dp[j][new_profit] + dp[j - g][k]) % MOD;
                }
            }
        }
        
        // The total number of profitable schemes is the sum of schemes with at least minProfit
        // for any number of members from 0 to n.
        long long total_profitable_schemes = 0;
        for (int j = 0; j <= n; ++j) {
            total_profitable_schemes = (total_profitable_schemes + dp[j][minProfit]) % MOD;
        }
        
        return total_profitable_schemes;
    }
};

Profitable Schemes in TypeScript

function profitableSchemes(n: number, minProfit: number, group: number[], profit: number[]): number {
    const MOD = 1e9 + 7;
    
    // dp[i][j] will be the number of schemes using i members to generate profit j.
    // The j=minProfit column is special: it stores schemes with profit >= minProfit.
    const dp: number[][] = Array(n + 1).fill(0).map(() => Array(minProfit + 1).fill(0));
    
    // Base case: There's one way to have 0 members and 0 profit (the empty scheme).
    dp[0][0] = 1;
    
    // Iterate through each crime.
    for (let i = 0; i < group.length; i++) {
        const g = group[i];
        const p = profit[i];
        
        // Iterate backwards for members and profits to use results from the previous state.
        for (let j = n; j >= g; j--) {
            for (let k = minProfit; k >= 0; k--) {
                // For each scheme with j-g members and k profit, we can add the current crime.
                // The new scheme will have j members and a new profit.
                const newProfit = Math.min(minProfit, k + p);
                dp[j][newProfit] = (dp[j][newProfit] + dp[j - g][k]) % MOD;
            }
        }
    }
    
    // The total number of profitable schemes is the sum of schemes with at least minProfit
    // for any number of members from 0 to n.
    let totalProfitableSchemes = 0;
    for (let j = 0; j <= n; j++) {
        totalProfitableSchemes = (totalProfitableSchemes + dp[j][minProfit]) % MOD;
    }
    
    return totalProfitableSchemes;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.